Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To create 2D array using NSMutableArrays you would need to do the following:</p> <pre><code>// 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 &lt; countRows; i++) { NSMutableArray* row = [NSMutableArray array]; [array2D addObject:row]; } </code></pre> <p>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.)</p> <p>To set a value at a specific row and column you would do the following:</p> <pre><code>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 &gt;= (rowIndex + 1)) { NSMutableArray* row = (NSMutableArray*)[array2d objectAtIndex:rowIndex]; // Get the 8th column // Make sure there are 8 columns NSUInteger countColumns = row.count; if (countColumns &gt;= (columnIndex + 1)) { // Set the value [row setObject:value atIndex:columnIndex]; } } </code></pre> <p>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.</p> <pre><code>int intValue = 1; NSNumber* number = [NSNumber numberWithInt:intValue]; BOOL boolValue = NO; NSNumber* number = [NSNumber numberWithBool:boolValue]; </code></pre> <p>NSArrays are not modifiable. If you need to add or remove objects to an array, you should you an NSMutableArray. Otherwise use a NSArray.</p> <p>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.</p> <p>You can't add nil objects to NSArrays and NSMutableArrays.</p> <p>NSMutableArray is dynamically resizing whilst C arrays are not. This makes it much easier to deal with adding and removing objects to the array.</p> <p>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:</p> <pre><code>int knownConstantValues[] = { 1, 2, 3 }; </code></pre> <p>You might need to use a C array to pass data to a library with a C API, e.g. OpenGL.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload