NSArray of int []

I need to save a bunch of int [] for my levels.

I decided that saving int [] in NSMutableArray and getting random from an array would be a good way to do this.

Thing is, int [] is not an object, and you cannot add it inside the obj-c array.

Does anyone have a suggestion on how I can get a random integer array?

My arrays look 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,...... }; 

Then I need to get random ones.

+6
arrays objective-c int iphone xcode
source share
4 answers

EDIT: modified to use the less vicious method, kindly provided by Tommy's comment.

You can treat 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 can wrap each individual array in your own NSData object and save them in a mutable array:

 [mutableArray addObject:[NSData dataWithBytes:lvl1 length:sizeof(int) * lengthOfArray]]; ... const int* level = (const int*) [(NSData*) [mutableArray objectAtIndex:whatever] bytes]; 

I have to agree with Frank C, but why do you need to use a Cocoa array to store these arrays at all? Can't you just treat the whole batch as an array of 2D C? If this is static data, then the dynamic aspects of NSMutableArray seem pretty redundant.

EDIT 2: Using C Arrays

If your level arrays are the same length - name it LEVEL_SIZE - you can build a direct 2D array as follows:

 static int levels[][LEVEL_SIZE] = { {1, 2, 3, 4, ...}, {15, 17, 19, 21, ...}, {8, 7, 6, 5, ...} }; 

Otherwise, you will need to build each array individually, and then connect them later:

 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, ...}; 

In any case, you can align one level as a pointer:

 int* level = levels[0]; printf("%d\n", level[1]); // should print 2 

To begin with, you will have the NUM_LEVELS level - in your case 50 - so hold as many 0..49 indices in 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 to count, retrieve, and delete needs. When you pull out an NSNumber object, use it to index into an array of levels to get the level:

 int* level = levels[[someNSNumber intValue]]; 

Et voila.

+9
source share

Use NSNumber to store int in NSMutableArray.

Here is a sample code:

 // Add [yourMutableArray addObject:[NSNumber numberWithInt:yourInt]]; // Get yourInt = [((NSNumber*)[yourMutableArray objectAtIndex:0]) intValue]; 
+8
source share

Craig

Since Objective-C is based on C, and you can mix in both iOS and OSX applications, why not just create an array of ints?

This will give you scale performance and memory reduction, rather than converting everything to NSNumber.

Franc

+4
source share

Paste it into NSNumber:

 [array addObject:[NSNumber numberWithInt:5]]; 
+2
source share

All Articles