Reputation: 14002
I am needing to store a heap of int[] for my levels.
I decided that storing the int[]'s in an NSMutableArray and getting a random one from the array would be a good way to do it.
Thing is, an int[] is not an object and you cannot add it inside an obj-c array.
Does anyone have a suggestion on how I can get a random integer array?
My arrays looks like this:
int lvl1[] { 4,80,49,6,40,77,21,20,91,5,100,91,...... };
int lvl2[] { 20,260,385,20,290,448,21,210,329,21,...... };
int lvl3[] { 441,21,90,364,21,70,385,21,170,434,...... };
...
int lvl50[] { 441,21,90,364,21,70,385,21,170,434,...... };
I then need to get a random one of these.
Upvotes: 8
Views: 30832
Reputation: 9543
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\n", 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.
Upvotes: 10
Reputation: 3130
Use NSNumber to store int in an NSMutableArray.
Here's sample code :
// Add
[yourMutableArray addObject:[NSNumber numberWithInt:yourInt]];
// Get
yourInt = [((NSNumber*)[yourMutableArray objectAtIndex:0]) intValue];
Upvotes: 9
Reputation: 8088
Craig,
As Objective-C is based on C and you can intermix both in iOS or OSX applications why not just create an array of ints?
This would give you the performance of scale and reduction in memory vs. converting everything to NSNumber.
Frank
Upvotes: 4
Reputation: 4649
Box it into a NSNumber :
[array addObject:[NSNumber numberWithInt:5]];
Upvotes: 3