Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing value of a NSNumber

Tags:

objective-c

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. 
like image 826
Boon Avatar asked Jul 01 '09 22:07

Boon


People also ask

Is NSNumber mutable?

All NSNumber instances are immutable, and there is no mutable subclass; if you need a different number, simply use another NSNumber instance.

Is NSNumber integer?

NSNumber provides readonly properties that return the object's stored value converted to a particular Boolean, integer, unsigned integer, or floating point C scalar type.

How do I know if NSNumber is nil?

The change dictionary passed on by observeValueForKeyPath:ofObject:change:context: may contain a null value that will not evaluate to 'nil'. Thus 'NSNumber *oldNumber = [change valueForKey:NSKeyValueChangeOldKey];' will pass the (oldNumber != nil) test but then something like 'oldNumber.


2 Answers

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

like image 163
mmc Avatar answered Oct 05 '22 15:10

mmc


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

See, for example, this post.

like image 23
Naaff Avatar answered Oct 05 '22 16:10

Naaff