Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

blue-lion

macrumors member
Original poster
Jan 26, 2010
50
0
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.
 
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).
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.