NSNumber query

Discussion in 'iOS Programming' started by blue-lion, Dec 31, 2010.

  1. blue-lion macrumors member

    Joined:
    Jan 26, 2010
    #1
    Hello,
    how can i change the contents of an NSNumber object easily? Is there a setter for this? I've noticed that when i step through my code with the debugger it seems to happily accept the following lines of code, although it's obviously the wrong thing to do.

    All i want to do is change the value from 100 to 200?


    NSNumber *myInt1 = [NSNumber numberWithInt: 100];
    NSNumber *myInt1 = [NSNumber numberWithInt: 200];

    Apart the ability to store these objects in arrays, I'm struggling at the moment to see why they're used at all??. I guess there must be a purpose ,which for the moment is beyond me. However they're mentioned in all the good objective c books so I'm going to persevere.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You cannot change them, only recreate them. They are used wherever Cocoa requires an object, such as in arrays or dictionaries. You should only use them when necessary, otherwise use the primitive C data types (int, char, long, etc).
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    This concept of mutable and immutable objects is used throughout the foundation container classes. Actually there's no mutable NSNumber class.

    The main purpose for NSNumber is to store values in the containers. I almost never use them for any other purpose and almost never have an ivar that's an NSNumber.
     

Share This Page