0

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?

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

oh damn, I feel silly. My array inits were in the wrong place. DOH!
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.
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.
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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.