0
- (void)setToInitialStateMain
{
    [super clearBoard];

    if (_data[@"StoneOne"] != nil) {
        NSDictionary* stoneOne = _data[@"StoneOne"];
        NSNumber* c = stoneOne[@"Column"];
        NSNumber* r = stoneOne[@"Row"];
        NSInteger column = [c intValue];
        NSInteger row = [r intValue];
        [_boardCol addObject:[NSNumber numberWithInt:column]];
        [_boardRow addObject:[NSNumber numberWithInt:row]];
    }
}

So the @"StoneOne", @"Column", and @"Row" keys are coming from an NSDictionary plist file. When I try to convert the NSNumber @"Column" to NSInteger, everything works ok.

Now, the line [_boardCol addObject:[NSNumber numberWithInt:column]]; is ok in terms of 'column' being the correct integer (2). But, when setup a breakpoint at the end of the method call to examine _boardCol (as well as _boardRow), both NSMutableArray* instance variables are reading nil. Why is that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Frank
  • 107
  • 9
  • 1
    were both of your NSMutableArrays initialized properly? – zfetters May 06 '16 at 20:46
  • How do you mean? I just wrote them as instance variables inside the curly braces of my @implementation Model file. Is that still not initializing them? – Frank May 06 '16 at 20:49
  • no you actually need to alloc and init them somewhere still before they can be used, more than likely just in the init method of you class – zfetters May 06 '16 at 20:51
  • Thank you zfetters, you're right...good call – Frank May 06 '16 at 20:53
  • Follow up question: I'm trying to access the objects of the _boardCol array in another method but can't seem to do that. The method call is coming from a different file whereas the call to the 'setToInitialStateMain' method is coming from the same file. – Frank May 06 '16 at 20:59
  • Why do you twice get the intValue (anyway, that should be integerValue) from an NSNumber and then create a new NSNumber? That's pointless. Just add the NSNumber. – gnasher729 May 06 '16 at 21:50
  • possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs May 07 '16 at 06:55

1 Answers1

0

All instance variables start out as nil. And a method call to nil returns nil (or does nothing). You need to instantiate the NSMutableArray yourself at some point. This is often done in -init, or inline code checking if the ivar is nil and if so allocating it:

if (self.boardCol == nil) {
    self.boardCol = [[NSMutableArray alloc] init];
}

The above is not a safe thing to do if multiple threads could be involved. It's often easier to just create them in your -init method.

Carl Lindberg
  • 2,902
  • 18
  • 22
  • That works well....however, when I try to access the objects from a different method (which is being called by another object in another file), I receive nil. Any thoughts? – Frank May 06 '16 at 21:07
  • show how you are setting up access to this class. – john elemans May 06 '16 at 22:09
  • If your method above is called first, it would work ;-) This is why the init method is easiest -- that will instantiate them before anyone else could access them. Or, override the getter method to do the above, so that anyone who calls the getter method will get it lazily created. – Carl Lindberg May 06 '16 at 22:15
  • @Frank I hope you would have declared "_boardRow" & "_boardCol" globally for accessing it in other classes as you mentioned & still if you access any of those variables if you are getting error try the following, 1.) Make sure you are accessing the variable after you assigned values to it. 2.) If you still get nil values, then initialise the arrays as in Carl Lindberg's answer in your "viewDidLoad" method, then it should be working fine. – Bharath May 07 '16 at 04:55