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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:18+00:00 2026-05-26T06:39:18+00:00

I am trying to comprehend how I can create multi-dimensional NSMutable arrays in general.

  • 0

I am trying to comprehend how I can create multi-dimensional NSMutable arrays in general. I have come across a few solutions but haven’t been able to make them work for me, so I am not sure of their validity. Now if only someone here can help me understand how to create 2-D NSMutable arrays better that would be great!

Moving to the next question, I am not sure when I should summon NSArray/NSMutableArray vs simply using a C array. In the particular case I am dealing with, I have a fixed data type
that I want to use (boolean values) and these are clearly not objects. NS and NSMutableArray are meant to hold objects, if I am not mistaken. So is this a good idea to use a regular C array vs NSMutable array?

Adding a final twist to the question on creating 2D arrays, is using NSMatrices a better alternative or even an option than creating 2D NSMutable arrays?

Thanks and major high fives to all those who read and answer this!

  • 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-26T06:39:19+00:00Added an answer on May 26, 2026 at 6:39 am

    To create 2D array using NSMutableArrays you would need to do the following:

    // Create the 2D array
    // array2D is created autoreleased. It should be retained somewhere
    // to keep it around. 
    NSMutableArray* array2D = [NSMutableArray array];
    
    // Add a NSMutableArray to array2D for each row
    NSUInteger countRows = 8; // Or whatever value you need
    for (NSUInteger i = 0; i < countRows; i++) {
      NSMutableArray* row = [NSMutableArray array];
      [array2D addObject:row];
    }
    

    Note that you can add additional rows to array2D at any time. Also each row starts out
    with size 0 and is empty. You can add different number of elements to each row so it
    is a jagged 2D array rather than something more like a matrix which would be fixed size
    (i.e. M rows x N columns.)

    To set a value at a specific row and column you would do the following:

    NSUInteger rowIndex = 5;
    NSUInteger columnIndex = 7;
    NSNumber* value = [NSNumber numberWithInt:11];
    
    // Get the 6th row
    // Make sure there are 6 rows
    NSUInteger countRows = array2d.count;
    if (countRows >= (rowIndex + 1)) {
      NSMutableArray* row = (NSMutableArray*)[array2d objectAtIndex:rowIndex];
      // Get the 8th column
      // Make sure there are 8 columns
      NSUInteger countColumns = row.count;
      if (countColumns >= (columnIndex + 1)) {
        // Set the value
        [row setObject:value atIndex:columnIndex];
      }
    }
    

    You can store C types in NSMutableArrays by wrapping them in ObjectiveC objects.
    A number can be translated into an object using NSNumber. NSNumber can also wrap boolean
    values. Pointers to C structs can be wrapped using NSValue objects. You can also create
    NSValue objects that wrap specific Cocoa types, e.g. CGRect.

    int intValue = 1;
    NSNumber* number = [NSNumber numberWithInt:intValue];
    
    BOOL boolValue = NO;
    NSNumber* number = [NSNumber numberWithBool:boolValue];
    

    NSArrays are not modifiable. If you need to add or remove objects to an array, you should you an NSMutableArray. Otherwise use a NSArray.

    NSMutableArrays and NSArrays retain the objects that are added to them, taking ownership of them. When an object is removed from an NSMutableArray it is released so that it is cleaned up. When you release an NSArray or an NSMutableArray that you no longer require, they will clean up any objects that you have added to the array. This makes memory management of
    objects within arrays much easier.

    You can’t add nil objects to NSArrays and NSMutableArrays.

    NSMutableArray is dynamically resizing whilst C arrays are not. This makes it much easier
    to deal with adding and removing objects to the array.

    I would use a C array for a group of C types, e.g. ints that is fixed size and
    whose values are known at compile time:

    int knownConstantValues[] = { 1, 2, 3 };
    

    You might need to use a C array to pass data to a library with a C API, e.g. OpenGL.

    Hope this helps.

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

Sidebar

Related Questions

I'm trying to comprehend the Unix file system on my OSX. I'm following wikipedia
I am trying to comprehend Perl following the way describe in the book Minimal
I'm trying to comprehend ontology basics. Here's an example: car (class) 2009 VW CC
I found this line of code and I'm trying to comprehend what it's doing.
Trying to make a make generic select control that I can dynamically add elements
I have been trying to figure out a way to convert bitmap files into
I am trying to comprehend the reference/dereference system in Perl. What I am trying
Trying to load a page fragment, or at least jump to a div but
I'm having an interesting but difficult problem with my JavaScript code. Basically, I'm trying
So I'm trying to comprehend the source file for csv2rec in matplotlib.mlab. It is

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.