开发者

Change NSNumber value by allocating new NSNumber?

开发者 https://www.devze.com 2022-12-24 22:37 出处:网络
I understand that NSNumber is immutable, but I still have the need to change it in my app. I use it because it\'s an开发者_运维问答 object, and therefore usable with @property (if I could, I would use

I understand that NSNumber is immutable, but I still have the need to change it in my app. I use it because it's an开发者_运维问答 object, and therefore usable with @property (if I could, I would use float or CGFloat in a heartbeat, but sadly, I can't).

Is there any way to just overwrite the old NSNumber instance? I'm trying this:

NSNumber *zero = [[NSNumber alloc] initWithFloat:0.0];
myNSNumber = zero

but this doesn't change the value. This is a simplified version of my code, as I'm changing the root view controller's parameter (I know, not MVC-friendly). Still, the changing of the value should be the same assignment, right? Should I release myNSNumber first? What should I set the @property at for myNSNumber?

Thanks!


You can get an autoreleased instance of NSNumber using convenience methods such as numberWithDouble: or numberWithInteger:. No need to invoke alloc/init, especially if you're assigning to a property with the retain specifier (these automatically retain/release upon assignment).


((myViewController*)self.parentViewController).otherViewController.value

Psychic debugging tells me that one of the objects in this chain of properties is nil. My money is on parentViewController because I often forget to set parent properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消