Malloc like this
int **terrain;
terrain = malloc(sizeof(int*) * mapSize.x);
for (int i = 0; i < mapSize.x; i++) {
terrain[i] = malloc(mapSize.y * sizeof(int));
}
Use it. Convert to NSdata like this before saving
NSData *data=[NSData dataWithBytes:terrain length:(30*sizeof(int*) +30*30*sizeof(int) )];
[rootObject setValue:data forKey:@"terrain"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
loading into NSdata then converting back to int**
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSData *data = [rootObject valueForKey:@"terrain"];
terrain =(int**) [data bytes];
With this code, is it saving the *int
addresses then when I load the data it does not point to the correct data any more?
Or do I have a problem with the "endianness" as described in
Documentation
If it is the address issue, should i put a for loop when saving to convert *int
to NSData then save all those and recreate the **int
with another for loop/malloc?