Why can't I add?

Discussion in 'Mac Programming' started by AussieNerd, Mar 20, 2010.

  1. AussieNerd macrumors newbie

    Joined:
    Mar 20, 2010
    #1
    Could someone please explain why I can't make an integer by adding together the return values that are both integers themselves?

    The range is a range that I have created from searching inside a string for a substring.

    NSUInteger myInteger = [aRange location] + [aRange length];
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Try this:

    NSUInteger myInteger = [aRange location];

    Does it compile? If not, then the problem is not with the adding, it is somewhere else. Did you read the error messages the compiler gave you? What exactly did they say?
     
  3. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #3
    If aRange is an NSRange, it's not an object; it's a C struct and [aRange location] *should* cause the compiler to howl in agony. Instead, you need to access the location and length fields using the standard C dot-notation:
    Code:
    aRange.location;
    aRange.length;
     
  4. AussieNerd thread starter macrumors newbie

    Joined:
    Mar 20, 2010
    #4
    Guiyon, you're a genius. Thanks. One of the most confusing thing about coming to iPhone development (or mac) is that it is a mash-up of different languages. Why not make it all work consistently? Could a range not be an object?
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    it's certainly not a mash-up of different languages. Objective-C is it, you can mix C++ if you really want to. Objective-C is a superset of C, but that doesn't make it a mash-up in my mind.

    NSRange is part of Foundation, which is a base framework providing C underpnnings and base classes. This provides low-level functionality. Foundation datatypes like this made it into Cocoa because they're already there, and serve the intended purpose. You could certainly have a range object, but it seems like overkill to me. What messages would you want a range to respond to? For simple pieces of data like this it seems better to me to have a structure, and not have to slow things down by incurring the overhead of an object, dealing with associated memory management, etc. when there aren't any real benefits.

    -Lee
     

Share This Page