Mac Objective C - NSUserDefaults with NSInteger not working

Narcs

macrumors newbie
Original poster
Jan 19, 2012
10
0
I am using NSUserDefaults to set an NSInteger
Code:
 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.
 
Last edited by a moderator:

jettoblack

macrumors member
Nov 1, 2006
70
0
Look carefully at these two lines and spot the difference:

Code:
[saveFile setInteger:2 forKey:@"saveKey"]; 
...
NSInteger saveInt = [saveFile integerForKey:@"integerKey"];
(Hint: What's the name of the key that holds your integer?)
 

Narcs

macrumors newbie
Original poster
Jan 19, 2012
10
0
overwriting NSInteger with NSUserdefaults

I am using this code to save an integer


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

And this to load it

NSUserDefaults *saveFile = [NSUserDefaults standardUserDefaults];
NSInteger saveInt = [saveFile integerForKey:mad:"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

Moderator
Staff member
Aug 9, 2009
8,824
5,245
vertical
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/...ns.html#//apple_ref/doc/uid/10000059i-CH2-SW6
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.