views:

66

answers:

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]; 
+2  A: 

Yikes! You've got all kinds of problems.

You need to go back to the drawing board and read up on some stuff.

  1. 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.

  2. A char is an integer type so you should be storing it as an NSNumber, or use the convenience method: -[NSUserDefaults setInteger:forKey:].

  3. 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.

  4. The warnings that you are getting will tell you what the problems are. Make sure you understand them.

  5. 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.

  6. 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. ;-)

Chris Suter
O thanks guys i'm new to objective c, trying to get a good grasp on it.
That last bit of snark probably wasn't necessary. It's hard enough to learn how to program without people making fun of you the whole time.
willc2