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.
All NSNumber instances are immutable, and there is no mutable subclass; if you need a different number, simply use another NSNumber instance.
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.
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.
NSNumber is immutable. Actually, it's a subclass of NSValue, and all NSValues are immutable.
No, you can't change the value of NSNumber.
See, for example, this post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With