Saving an int to nsuserdefaults

Discussion in 'iOS Development' started by JesseEtzler, Jul 17, 2009.

  1. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch
    I've been trying many different ways to save an int to my nsuserdefault with no luck at all :/ I've followed tutorials and everything but nothing seems to work... I never get any errors it just never saves my int.I have an int highscore = 0; which gets updated when you achieve a highscore but it just won't save to nsuserdefaults no matter what code I use. Any help ?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [OBJC]NSNumber *number = [NSNumber numberWithInt:yourIntValue];
    [[NSUserDefaults standardUserDefaults] setObject:number forKey

    Please Register or Log in to view images

    "YourKey"];[/OBJC]

    NSUserDefaults can only store objects. So convert your integer to a NSNumber object and set that. Recall it with objectForKey.
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks Skylar. I was also wondering about this since my current app that I'm working on will have high scores.
  4. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Really?

    Because I made an "app" a couple days ago which stores an int to the standard user defaults, and it worked...

    I didn't use setObject though, I used this:
    [[NSUserDefaults standardUserDefaults] setInteger:topScore forKey

    Please Register or Log in to view images

    "TopScore"];
    topScore was an int.
  5. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch
    oh didn't know it needed to be converted to a NSNumber.. I will see if this works.

    edit.

    Ya forgot to ask about loading as well. Do I need something like this in my superviewdidload NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; along with something to load the int I saved ?
  6. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You don't.

    When you do:
    [objc][[NSUserDefaults standardUserDefaults] somethingHere:withObject][/objc]
    You are sending that message to an object that already exists.

    And did you see my previous post?

    I used:
    [objc][[NSUserDefaults standardUserDefaults] setInteger:topScore forKey

    Please Register or Log in to view images

    "TopScore"];[/objc]

    And it worked like charm.
  7. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch

    ya idk whats up :/ no errors yet again... but no saving when I exit and load my game back up. It's really wiered. Are there any example applications out there that save an int to nsuserdefaults ?
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129

    You're absolutely right. I keep limiting NSUserDefaults to a NSDictionary in my head. Which is absolutely wrong because NSUserDEfaults isn't even an NSDictionary. Thanks for the catch.
  9. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    What I wrote before is copied and pasted from my working code:
    Writing:
    [objc][[NSUserDefaults standardUserDefaults] setInteger:topScore forKey

    Please Register or Log in to view images

    "TopScore"];[/objc]

    Reading:
    [objc]topScore = [[NSUserDefaults standardUserDefaults] integerForKey

    Please Register or Log in to view images

    "TopScore"];[/objc]

    And you should open the NSUserDefaults.h file and check the methods, it's way better than asking and waiting for a reply.
  10. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Add a printf or NSLog statement in the method, just to make sure it's getting called.

Share This Page