tags:

views:

196

answers:

2

Is there a way to change the value contained in an NSNumber after it is created without making it point to a different NSNumber?

NSNumber *num = [NSNumber numberWithInt:0];
num = [NSNumber numberWithInt: 1]; // now num points to a different object, which I don't want.  I want it the same object still, but different value.
+4  A: 

No, you can't change the value of NSNumber.

See, for example, this post.

Naaff
+4  A: 

NSNumber is immutable. Actually, it's a subclass of NSValue, and all NSValues are immutable.

mmc
It is a bit odd that there is no NSMutableNumber
Peter N Lewis
Not really, typically when you'd want a "mutable" number you end up using a primitive. NSNumber is more useful for serialization, being a dictionary key, and things along those lines.
Marc Charbonneau