EDIT: changed to use a less evil method courtesy of Tommy's comment.
You can treat the static arrays as pointers and store them in NSValue
objects:
[mutableArray addObject:[NSValue valueWithPointer:lvl1]];
...
int* level = [(NSValue*)[mutableArray objectAtIndex:whatever] pointerValue];
int someContainedInt = level[index];
Alternatively, you could wrap each individual array in its own NSData
object and store those in the mutable array:
[mutableArray addObject:[NSData dataWithBytes:lvl1 length:sizeof(int) * lengthOfArray]];
...
const int* level = (const int*) [(NSData*) [mutableArray objectAtIndex:whatever] bytes];
I have to concur with Frank C, though -- why do you need to use a Cocoa array to store these arrays at all? Can't you just treat the whole lot as a 2D C array? If it's static data anyway then the dynamic aspects of NSMutableArray
seem pretty much superfluous.
EDIT 2: Using C arrays
If your level arrays are all the same length -- call it LEVEL_SIZE
-- you can build a straight 2D array like this:
static int levels[][LEVEL_SIZE] =
{
{1, 2, 3, 4, ...},
{15, 17, 19, 21, ...},
{8, 7, 6, 5, ...}
};
Otherwise, you'll need to build each array separately and then put them together afterwards:
static int level1[] = {1, 2, 3, 4, ...};
static int level2[] = {15, 17, 19, 21, ...};
static int level3[] = {8, 7, 6, 5, ...};
...
static int* levels[] = {lvl1, lvl2, lvl3, ...};
Either way, you can pluck out one level as a pointer:
int* level = levels[0];
printf("%d
", level[1]); // should print 2
To start with, you'll have NUM_LEVELS
levels -- in your case 50 -- so stick that many indices 0..49 into your mutable array, as NSNumber
objects:
NSMutableArray* levelIndices = [NSMutableArray arrayWithCapacity:NUM_LEVELS];
for ( int i = 0; i < NUM_LEVELS; ++i )
[levelIndices addObject:[NSNumber numberWithInt:i]];
Use this array for your counting, getting and removing needs. When you pull an NSNumber
object out, use it to index into the levels array to get the level:
int* level = levels[[someNSNumber intValue]];
Et voila.