0

I'm trying to create a NSMutableArray within a function that continuously runs, and initialize its values just once so that it doesn't keep initializing the values (thus replacing the changed values) as the function is repetitively called. My problem is when I try to initialize the values in an if statement the values in the array won't change, it keeps printing "value is 0" when I'm expecting it to be printing "value is 1"

Here's my relevant code:

 @property (nonatomic, strong) NSMutableArray * shapeMarked;
 @synthesize shapeMarked;

 //locationManager is the function that's continuously called

 -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
 {

 //event count is an integer that continuously increases by one each time the 
 //parent function is called, this if statement is used so that it only happens 
 //once

 if(eventcount == 1){

    for (int i = 0; i < 5; i++){

        BOOL b = YES;

        [shapeMarked addObject:[NSNumber numberWithBool:b]];

        NSLog(@"value is %d", [[shapeMarked objectAtIndex:i] boolValue] );

    }

  }
 }
user671891
  • 165
  • 1
  • 3
  • 14
  • 2
    Is `eventcount == 1` ever matched? Add a NSLog in that if-case. Also, is shapeMarked actually a valid NSArray? Add a `NSLog(@"array: %@", shapeMarked);` – Till Apr 26 '12 at 20:14
  • When I added the NSLog(@"array ... ); it's outputting "array: (null). I know that "event count == 1" is matched because NSLog outputs, if it wasn't matched I wouldn't get the output because NSLog is called within the if statement. – user671891 Apr 26 '12 at 20:22
  • 1
    possible duplicate of [NSMutableArray addObject not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) – jscs Apr 26 '12 at 20:36

2 Answers2

1

Alloc and init the array somewhere! Did you?

self.shapeMarked = [NSMutableArray array];

eg in your init method should do. Without it, your shapeMarked is just nil.

Mario
  • 4,530
  • 1
  • 21
  • 32
  • Is there a way I can do that outside of any functions after the "@property" or "@synthesize" because when I try to do that on the "@property" line it gives me errors. – user671891 Apr 26 '12 at 20:26
  • 3
    You need to go and read the basic Objective-C guide; understand methods and classes, designated initializers and the like.... then the answer will be obvious (and you'll have a foundation for success). – bbum Apr 26 '12 at 20:28
  • As I wrote, try your init method. Also release the object in dealloc. Bbum's advice is sure a good one. – Mario Apr 26 '12 at 20:34
1

Your array obviously is not a valid NSMutableArray instance - or in other words, it is just nil.

That is the problem of your code.

When invoking a selector on a nil object, the return value always is nil (for objects) or 0 (for scalar types). Invoking objectAtIndex: will result into a returned nil. You are expecting a NSNumber instance and as said before, that will be nil. Now you are invoking boolValue on that nil instance and that will return 0 as you are expecting a scalar data type. See Apple's excellent Objective-C documentation.

You most likely have forgotten to initialize shapeMarked with a valid NSMutableArray instance.

Till
  • 27,559
  • 13
  • 88
  • 122