Reputation: 1
I'm trying to store different data types in a NSDictionary to save in NSUserdefaults when the game terminates. I'm trying to store a char, 3 floats and a string, I keep getting a warning on the char and the floats and I cant seem to find the answer anywhere. 1)Do i even need to setup the arrays? 2) How do I store the different data types to an object like an array or dictionary? the code looks like the this:
gameKeys = [[NSArray alloc] initWithObjects:@"gameScore",@"gameSound",@"gameDifficulty",@"theGameLoopSpeed",@"theDelayGameSpeed",nil];
gameValues = [[NSMutableArray alloc] init];
[gameValues setValue:score forKey:@"gameScore"];
[gameValues addObject:[NSString stringWithFormat:score]];// unsigned char
[gameValues addObject:[NSString stringWithFormat:sound]];//string
[gameValues addObject:[NSString stringWithFormat:gameDifficulty]];// char
[gameValues addObject:[NSNumber numberWithFloat:gameLoopSpeed]];// float
[gameValues addObject:[NSNumber numberWithFloat:delayGameLoopSpeed]];//float
NSDictionary *gameDict = [[NSDictionary alloc] initWithObjects:gameKeys forKeys:gameValues];
[gameDict setObject:[NSString stringWithFormat:score] forKey:@"gameScore"];//unsigned char
[gameDict setObject:[NSString stringWithFormat:sound] forKey:@"gameSound"];//string
[gameDict setObject:[NSString stringWithFormat:gameDifficulty] forKey:@"theGameDifficulty"];//char
[gameDict setObject:[NSNumber numberWithFloat:gameLoopSpeed] forKey:@"theGameLoopSpeed"];//float
[gameDict setObject:[NSNumber numberWithFloat:delayGameLoopSpeed] forKey:@"theDelayGameLoopSpeed"];//float
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
userDefaults = gameDict;
[userDefaults synchronize];
Upvotes: 0
Views: 2853
Reputation: 2927
Yikes! You've got all kinds of problems.
You need to go back to the drawing board and read up on some stuff.
If you use stringWithFormat, you must pass in a format string which will almost always be a string literal. If the variable that you're passing as an argument contains percent characters, your program will crash.
A char is an integer type so you should be storing it as an NSNumber, or use the convenience method: -[NSUserDefaults setInteger:forKey:].
userDefaults = gameDict isn't doing what you think it is. userDefaults is a pointer and a local variable. It's not assigning the user defaults. You need to understand pointers and the C language.
The warnings that you are getting will tell you what the problems are. Make sure you understand them.
I don't understand why you're initialising a dictionary and with some values and then trying to set them all over again. In any case, to make changes to a dictionary, it needs to be mutable, i.e. an instance of NSMutableDictionary.
It should be "gameLoopSpeed" not "theGameLoopSpeed".
It should look something like:
NSUserDefaults *dflts = [NSUserDefaults standardUserDefaults];
[dflts setInteger:score forKey:@"gameScore"];
[dflts setObject:sound forKey:@"gameSound"];
[dflts setFloat:gameLoopSpeed forKey:@"gameLoopSpeed"];
…
And you only need the synchronize call if your program is going to terminate abnormally soon after, which in your case, judging from what I've just seen, probably will. ;-)
Upvotes: 3