Boon
Boon

Reputation: 41510

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.

Upvotes: 33

Views: 20325

Answers (2)

mmc
mmc

Reputation: 17414

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

Upvotes: 46

Naaff
Naaff

Reputation: 9333

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

See, for example, this post.

Upvotes: 10

Related Questions