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 5976269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:12:15+00:00 2026-05-22T21:12:15+00:00

I am currently trying to write a class to create faux grid system to

  • 0

I am currently trying to write a class to create faux grid system to keep track of a NSMutableArray of game entities using a NSMutableArray of NSMutableArrays. Given my limited experience with Objective-C programming, I am unsure of how certain things work.

Here is the init method:

#define MAX_BALL_ROWCOUNT 6
#define MAX_BALL_COLCOUNT 4
- (id) initWithMutableArray:(NSMutableArray *)aList {
self = [super init];
if (self != nil) {
    ballList = [[NSMutableArray alloc] initWithCapacity: MAX_BALL_ROWCOUNT];

    for (int i=0; i<MAX_BALL_ROWCOUNT; i++) {
        NSMutableArray *balls = [[NSMutableArray alloc] initWithCapacity:MAX_BALL_COLCOUNT];
        [ballList addObject:balls];
        [balls release];
    }

    int x = 0;

    for (NSMutableArray *array in ballList) {
        for (int i = 0; i<MAX_BALL_COLCOUNT; i++) {
            [array addObject:[aList objectAtIndex:x]];
            x++;
            }
        }
    }

return self;
}

ballList is the class’s NSMutableArray that will store NSMutableArrays.

aList is the NSMutableArray containing the GameEntities I wish to keep track of that is passed into this class.

All the sizes and amount of entities to store are fixed, which is why there is no checks on the sizes of the arrays nor the number of entities to store.

So the first question I have involves freeing memory. This is the dealloc function I currently have:

- (void) dealloc {
    [ballList release];
    [super dealloc];
}

Does calling a release on ballList cause the release to be called on the NSMutableArrays that it contains (which will subsequently call the release on the objects those NSMutableArrays contain) or do I have to write something like:

for (NSMutableArray *array in ballList) {
    [array release];
}
[ballList release];

My second question involves the usage of this array of arrays. Is this the proper way to traverse through ballList?

- (void) update {
    for (NSMutableArray *array in ballList) {
        for (GameEntity *balls in array) {
            (CGPoint) location = [balls getLocation];
            [balls setLocation: CGPointMake(location.x+1, location.y+1)];
        }
    }
}

Lastly, in the code above where it sets the balls location, does it only affect the contents within ballList or does the original aList that is passed into ballList change as well? If the contents in the original aList do not change, how would I write it so that they do?

If people have suggestions for a better way to keep track of the entities in a grid system, I’d be open to those too. Thanks in advance.

  • 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-22T21:12:16+00:00Added an answer on May 22, 2026 at 9:12 pm
    1. When the dealloc of a NSArray or NSMutableArray is called, all its contents gets a release message. So when you release ballList, if there there is no other owner (I guess in this case there is none) then its dealloc is called and you don’t need to release the other arrays here.

    2. Your loop traversal is fine. Though for 2D arrays instead of NSArray of NSArray I personally prefer pure C 2D array, at least in most of the cases.

    3. When you are adding object in this way you are adding a reference in the array. So any change via the array’s reference will be reflected in all references of the object. If you don’t want that then add a copy of the object in the array.

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

Sidebar

Related Questions

I am currently trying to create a menu system for a game and cannot
currently I am trying to write to an 20x4 LCD Dot-Matrix Module using Perle's
I'm currently trying to create a ControlTemplate for the Button class in WPF, replacing
I'm currently trying to create a class which implements IEnumerable<T> in order to construct
I am currently trying to draw an object using VBOs. In the Cube-Class I
I am currently trying to write functional tests for a charging form which gets
I am currently trying to write an addin for PowerPoint that whenever any PowerPoint
Currently I'm trying to write my first Python library and I've encountered the following
i'm trying to write an unit test for my Android's RelativeLayout. Currently, my testcode
I'm trying to write the equivalent of strchr, but with NSStrings... I've currently got

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.