views:

70

answers:

3
+1  Q: 

Array of Objects

Complete and utter neophyte to Objective-C and the entire Mac platform so don't flame me please =). Basically I'm trying to create a simple game. The game has a board which I've created a class for and a board is comprised of squares which I also created a class for (board and square respectively).

In my view controller I'm trying to instantiate a board and add boardSize^2 squares to said object. board contains an NSMutableArray *squares.

I've also created a convenience method which sets an NSNumber *boardSize called initWithDimension.

In my touchesBegan handler I have the following:

board *game_board = [[board alloc] initWithDimension:10];   
int size = [game_board.boardSize intValue];

for(int i = 0; i <= size; i++) {
    square *s = [[square alloc] init];
    [game_board.squares addObject:s];
    [s release];
}

NSLog(@"%i", size);
NSLog(@"%@", [game_board.squares objectAtIndex:0]);

...and I'm getting 10 (as expected) and then (null). This is probably glaringly obvious to an experienced developer, I've just struggled for an hour trying to solve it and have given up. I've tried it without the [s release] as well, same result. I've also imported square.h and board.h.

Any ideas what's wrong here? Any other comments on what I'm brutalizing?

Thanks.

+3  A: 

Are you allocating and initializing your squares variable in initWithDimension?

fsmc
or subclassing the initializer method correctly?
JoePasq
In short, `game_board.squares` is almost certainly nil. All messages to nil return nil.
Chuck
Facepalm. Thank you.
James
+1  A: 

My guess is that game_board.squares is null. So passing a message to it (addObject) does nothing inside the loop. And objectAtIndex does nothing either.

Can you post your init code from square class?

Pablo Santa Cruz
+1  A: 
  1. The core problem you're facing is this: in the code you posted, you never assign an object reference to game_board.squares, so it's implicitly initialized to nil. When you call addObject: on nil, nothing happens. This is defined behavior within the Objective-C language. So, you should add an assignment of the form squares = [[NSMutableArray alloc] initWithCapacity:boardSize*boardSize]] before adding objects.

  2. You should be initializing your board within the initWithDimensions: method. That's the whole point of creating a class to begin with - encapsulating object behavior. Since the Board is the manager of the Square objects, it should initialize and manage them.

  3. Stylistically, class names are preferably Pascal-cased, meaning that each word is upper-cased: MyClassName. (In this case, prefer Board to board and Square to square.)

warrenm
You all nailed the obvious. Thank you very much. Baby steps here =).
James
@warrenm all good points. Thank you. I'm at proof-of-concept level at this point but #2 makes perfect sense.
James
Yeah, I wouldn't normally have gone that in-depth, but since you asked for help beyond just the central question, I figured I'd try to provide some helpful style guidelines.
warrenm
+1 Excellent answer. FWIW, this should probably be the accepted answer. @warrenm is very specific and adds good suggestions above and beyond the original question. His suggestion to encapsulate all of this behavior inside `-[Board initWithDimension:]` is particularly good. A touch handler method shouldn't have to know or care about the details of initializing a game board.
Quinn Taylor
Agreed and done.
James