Objective C - NSUserDefaults with NSInteger not working

Discussion in 'Mac Programming' started by Narcs, Jan 27, 2012.

  1. Narcs, Jan 27, 2012
    Last edited by a moderator: Jan 27, 2012

    macrumors newbie

    Joined:
    Jan 19, 2012
    #1
    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.
     
  2. macrumors member

    Joined:
    Nov 1, 2006
    #2
    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?)
     
  3. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    This is why you should be using constants for key values. See this question on stack overflow: Best implementation for global constants for keys.

    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?
     
  4. thread starter macrumors newbie

    Joined:
    Jan 19, 2012
    #4
    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 :)
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    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
     

Share This Page