Setting NSNumber

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Code:
-(IBAction)itemPhotoSwapBut:(id)sender{ 
    NSInteger tag = [sender tag];
    NSArray *photoNumArray = @[photo1selected,photo2selected,photo3selected,photo4selected]; // array of global NSNumbers instantiated to 0
    butImageCounter = 0;
   [[photoNumArray objectAtIndex:tag]setIntValue:butImageCounter];
}
Error
[__NSCFNumber setIntValue:]: unrecognized selector sent to instance 0x27
I seem to be struggling with this. I have 4 NSNumber objects in the header. in my awakeFromNib I assign them all to an int value of 0. I have 4 NSButtons assigned to this one method and use the [sender tag] to identify which button is pushed. In the example above I am trying to store the int value of 0 but I am getting this crash error. The error seems to indicate it is no longer in memory?
 

Sydde

macrumors 68020
Aug 17, 2009
2,105
2,163
IOKWARDI
NSNumber is immutable. If you want to change the value it holds, you must discard the object and re-init a different one.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
NSNumber is immutable. If you want to change the value it holds, you must discard the object and re-init a different one.
A quick web search confirmed it is not mutable. I did't think to even look at that as an answer to my problem. Thanks. From my experience that error message means the object does not exists.
 

Sydde

macrumors 68020
Aug 17, 2009
2,105
2,163
IOKWARDI
The reason you are seeing that odd 0x27 pointer is because there are some factory-like instances of NSNumber for the common values like zero or seven or whatever. All four of your numbers that you initialized to 0 will be the same object.

Really, if you are not encoding the value or putting it in an array, just use a NSInteger primitive.