views:

64

answers:

3

umm So simple question here:

I have an instance of NSMutableArray declared in my header

NSMutableArray *day19;

@property (nonatomic, retain) NSMutableArray *day19

implementation:

@synthesize day19;

In my viewDidLoad

self.day19 = [[NSMutableArray alloc] init];

In the myMethod where I want to add objects to the array I:

NSObject *newObject = [[NSObject alloc] init];

[day19 addObject:newObject];

However... when i check the day19 array there is nothing in it. If I conversely add the newObject to a tempArray within the myMethod scope and then set the day19 array to the tempArray, day19 has the objects.

Super basic I know just must be a confused morning or something...

thanks for any help

+1  A: 

Is day19 actually an instance variable? In the snippet, it's not clear when it's declared as an instance variable or just as a variable outside the scope of the class.

mipadi
+1  A: 

A couple of things:

Are you sure viewDidLoad is the right place to init your array? Confer here.

Also, at least from the code you've got posted, it looks like you're being sloppy with your retains. If your property is a retain type, you should not be writing:

self.myProperty = [[Something alloc] init]; // double retain here, bad

You should instead be writing something like:

self.myProperty = [[[Something alloc] init] autorelease]; // single, good

Also, with

NSObject *newObject = [[NSObject alloc] init];
[day19 addObject:newObject];

unless you have a

[newObject release];

down the pike, you've got a memory leak.

Clay Bridges
A: 

In my viewDidLoad

self.day19 = [[NSMutableArray alloc] init];

In the myMethod where I want to add objects to the array I:

NSObject *newObject = [[NSObject alloc] init];

[day19 addObject:newObject];

However... when i check the day19 array there is nothing in it. If I conversely add the newObject to a tempArray within the myMethod scope and then set the day19 array to the tempArray, day19 has the objects.

Let me guess: You checked the array with code like this:

NSLog(@"day19 contains %lu objects", [day19 count]);

Remember that a message to nil does nothing and returns nil, 0, or 0.0. That's why the output said 0 objects: You don't have an array in the first place. The most probable reason for that is that viewDidLoad hasn't been called yet, so you have not yet created the mutable array.

It's also possible that you have an array (i.e., the view has been loaded) at the time you examine the array, but you didn't have an array yet (the view hadn't been loaded yet) at the time you tried to add to the array, so your addObject: message fell on deaf ears.

Consider creating the array earlier. You probably should be creating it in init or initWithCoder:.

A third possibility is that you examined the array before you ever added to it. Make sure you log or break at both points, so you know which one happened first.

Whatever the problem is, you also need to either assign the array to the instance variable, not the property, or autorelease the array before assigning it to the property. Otherwise, you're over-retaining the array, which means you will probably leak it later on. You probably need to review the Memory Management Programming Guide for Cocoa.

Peter Hosey
once again with the great answer. I had actually figured it out previously but this rightly explains my error. I was running the method to add objects to my arrays before actually alloc'in and init'n them in my ViewDidLoad method. Simple copy paste error. Thanks for the great answer.
theprojectabot