views:

61

answers:

2

hi!

i have a problem with saving something and am officially out of ideas. what i want to do is save an integer array into the userDefaults, then when restarting the app loading this array and restart the game from this last point.

what i do is that after each turn my AI logic saves the actual game state into an array -> so far so good this works fine. the array is saved and when i "re"read the saved array it seems ok... well actually it has saved the correct array!

now i restart the game. i check for a bool variable that i also saved if the game was interrupted out of the orderly. works fine too.

now the trick part. i read the array from the userDefaults and WOHOOO! i get a slighty different array then i saved! i really really cant figure out why :(

well: now the code:

here is what i do to save the board:

NSData *data = [NSData dataWithBytes:&boardToSave length:sizeof(boardToSave)];
[userDefaults setObject:data forKey:@"MB_boardSave"];

and here what i do to get the data back when initialising the board:

int loadBoard[8][8] = {0};
NSData *data = [userDefaults objectForKey:@"MB_boardSave"];
memcpy(&loadBoard, data.bytes, data.length);

again... whats going in is correct, whats coming out is not :(

any suggestions would be very much appreciated! i´m also open to alternative saving options if you think there are better ones!

thx

+1  A: 

edit the bug is that you memcpy to &loadBoard. It should be plain loadBoard, since that is a pointer. Same goes for boardToSave.

The funny thing is that it does kind of work (and not crash) as you do it.

mvds
i didnt but it doenst work anyhow :( sizeof() and .length do return the same value (see below)
zwadl
oh and boardToSave is indeed defined int boardToSave[8][8]
zwadl
:) it does not crash either way but even that eay of writing it produces the same thing... right ones go in wrong ones come out :(
zwadl
mvds
your answer is certainly correct, but please see mine and have a good laugh :)
zwadl
A: 

im terribly sorry but its code blindness struck me :)

where i wanted this

if(loadBoard[j][i] == 1 || loadBoard[j][i] == 11 || loadBoard[j][i] == 12){
 [...];
}

i had this

if(loadBoard[j][i] == 1 || boardSetup[j][i] == 11 || boardSetup[j][i] == 12){
 [...];
}

no wonder it didnt work :) BUT for all that its worth there was some knid of mistake with saving and loading the array as well after i had the right results i was able to find the above "eye"bug :)

what i did now to save the array is to not use an int[8][8] array but an NSMutableArray that i save directly into the userDefaults. (no more memcopy and so on...) here is what i did and what (finally) works for me

save:

NSMutableArray* array = [[NSMutableArray alloc] init];
// ... fill Array like so: [array addObject:[NSNumber numberWithInt:11]]; ...
[userDefaults setObject:array forKey:@"MB_boardSave"];
[userDefaults synchronize];

load:

NSArray* array = [userDefaults objectForKey:@"MB_boardSave"];
int example = [[array objectAtIndex:0] intValue];

maybe thats usefull for someone else as well!

zwadl
well done, it's the less hacky way of doing things!
mvds
btw do you `release` the array? I suggest an `autorelease` after the `init`.
mvds
i did release it but changed to autorelease. thx for the hint!
zwadl