1

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.

1
  • 1
    Just a stylistic note: In Objective-C, it's a universal convention that names are camelCase and class names are capitalized. It's really awkward to read code where class names are lowercase, because my brain automatically thinks, "Wait, what is square? Is it a dynamically accessed class? Is he sending alloc to an instance?" Commented May 18, 2010 at 1:26

3 Answers 3

3

Are you allocating and initializing your squares variable in initWithDimension?

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

1 Comment

In short, game_board.squares is almost certainly nil. All messages to nil return nil.
2
  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.)

4 Comments

You all nailed the obvious. Thank you very much. Baby steps here =).
all good points. Thank you. I'm at proof-of-concept level at this point but #2 makes perfect sense.
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.
+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.
1

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?

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.