Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 794771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:22:04+00:00 2026-05-14T22:22:04+00:00

Complete and utter neophyte to Objective-C and the entire Mac platform so don’t flame

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:22:05+00:00Added an answer on May 14, 2026 at 10:22 pm
    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.