PDA

View Full Version : Objective C - NSUserDefaults with NSInteger not working




Narcs
Jan 27, 2012, 08:39 AM
I am using NSUserDefaults to set an NSInteger

NSUserDefaults *saveFile = [NSUserDefaults standardUserDefaults];
[saveFile setInteger:2 forKey:@"saveKey"];

This is working as a save game function.
Then in another view controller I am using the following code to load the game


NSUserDefaults *saveFile = [NSUserDefaults standardUserDefaults];
NSInteger saveInt = [saveFile integerForKey:@"integerKey"];

if (saveInt == 2) {
L2Raul1ViewController * raul1 = [[L2Raul1ViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:raul1 animated:YES];
}

But for some reason this isn't working. Keep in mind this is being done in different view controllers, and it needs to be done in many other view controllers.



jettoblack
Jan 27, 2012, 08:57 AM
Look carefully at these two lines and spot the difference:

[saveFile setInteger:2 forKey:@"saveKey"];
...
NSInteger saveInt = [saveFile integerForKey:@"integerKey"];

(Hint: What's the name of the key that holds your integer?)

GorillaPaws
Jan 27, 2012, 09:13 AM
This is why you should be using constants for key values. See this question on stack overflow: Best implementation for global constants for keys (http://stackoverflow.com/questions/538996/constants-in-objective-c/539191#539191).

Also you should have detected the error if you were setting breakpoints on the values. Are you familiar with running the debugger and using breakpoints?

Narcs
Jan 27, 2012, 08:06 PM
I am using this code to save an integer


NSUserDefaults *saveFile = [NSUserDefaults standardUserDefaults];
[saveFile setInteger:2 forKey:@"saveKey"];

And this to load it

NSUserDefaults *saveFile = [NSUserDefaults standardUserDefaults];
NSInteger saveInt = [saveFile integerForKey:@"saveKey"];

This works, now my question is how could reset this same integer. I can't use the save code again because it makes a new integer. Keep I am using this integer throughout multiple view controllers. Thanks :)

chown33
Jan 28, 2012, 11:58 AM
This works, now my question is how could reset this same integer. I can't use the save code again because it makes a new integer. Keep I am using this integer throughout multiple view controllers. Thanks :)

Please explain exactly what you mean by "reset this same integer" and "makes a new integer". Please use the standard terms, like "key" and "value", or "name", which means the same thing as "key".

By "reset this same integer", do you mean you want to delete the key and its associated integer value from defaults? Do you mean set the integer value to zero? Do you mean something else?

I can't even guess what you might mean by "makes a new integer". If you set another integer for the same key, the new value replaces the old one. There's no "making" of a "new integer".

If you want a completely separate integer value, then use a different key. NSUserDefaults is like an NSDictionary in that way. Different keys have separate values.

You should probably read this:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html%23//apple_ref/doc/uid/10000059i-CH2-SW6