Setting NSNumber

Discussion in 'Mac Programming' started by larswik, Jun 30, 2015.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
    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?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    NSNumber is immutable. If you want to change the value it holds, you must discard the object and re-init a different one.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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.
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    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.
     

Share This Page