tags:

views:

607

answers:

2

Hi, I am learning Objective-C and have hit a snag.

I have created a custom object called "Baddie". I am trying to add the object to a couple of mutable arrays.

Baddie *b1 = [[Baddie alloc]initWithTag:4444 pos:cpv(450,270) dir:cpv(0,1)]; 
[baddieArray addObject: b1];    
[baddieWaitingArray addObject: [[Baddie alloc]initWithTag:4447 pos:cpv(450,270) dir:cpv(0,1)]];
[baddieWaitingArray addObject: [[Baddie alloc]initWithTag:4448 pos:cpv(450,270) dir:cpv(0,1)]];
    //end

At "end" both arrays appear to be empty. The arrays were previously initialised as follows:

baddieArray = [[NSMutableArray alloc] init];
baddieWaitingArray = [[NSMutableArray alloc] init];

I'm not sure why the arrays are empty. The pointer to b1 is still pointing to the object. I can still retrieve properties from it. Where have I gone wrong?

+1  A: 

This seems too simple, but if baddieArray is nil, then the adds will be ignored and querying its size will return zero.

I realize that you show reasonable allocation for the arrays, but could something have happened to them after that point?

Darron
oh damn, I feel silly. My array inits were in the wrong place. DOH!
deadcat
As a proponent of the "please kick me if I do something wrong" school, the ability to successfully send messages to nil in Objective-C bothers me. This type of confusion results.
Darron
Sending a message to nil is not necessarily incorrect, though. There is a cost to the code bloat of thousands of nil checks that exist for no other reason than because nil throws an exception in your language.It is possible, though, to make nil respond differently in Objective-C.
Chuck
I understand that allowing messages to nil fits the Objective-C dynamic style. I just come from a background where I usually prefer non-dynamic languages because I need things to fail early and noisily.
Darron
+1  A: 

Hi,

Have you placed a breakpoint near //end and checked that the array's are not nil?

Bill Dudney
yes, they are shown as having 0 objects in them.
deadcat