Monday, February 7, 2011

Changing value of a NSNumber

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.
  • No, you can't change the value of NSNumber.

    See, for example, this post.

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

    Peter N Lewis : It is a bit odd that there is no NSMutableNumber
    Marc Charbonneau : 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.
    From mmc

0 comments:

Post a Comment