Saving Integers

Discussion in 'iOS Programming' started by Wellington2k, Aug 19, 2011.

  1. Wellington2k macrumors regular

    Joined:
    Jun 4, 2011
    #1
    Hello. I'm trying to make it where every time you open the app it adds 1 to an integer. I need the integer to save it's value to a plist or a txt or what ever is the best. I've learn a lot more about objective-C than the last time I wrote, but I'd still like to know what file to put it in.
    Thanks.
     
  2. xcodeNewbie macrumors member

    Joined:
    Jul 1, 2011
    #2
    Save integers.

    The easiest way to save small data, like an integer, is with NSUserDefaults.
    Code:
     [[NSUserDefaults standardUserDefaults] setInteger:myInt forKey:@"myKey"];
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    The "forKey" part gives you a method for looking up this number later, similar to an NSDictionary. The "synchronize" part saves it to memory. When you want to recall your integer, do this:

    Code:
     int myInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"myKey"]; 
    Hope this helps!
     
  3. Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #3
    I get three errors.

    Code:
    #import "SaveTestViewController.h"
    
    @implementation SaveTestViewController
    
    int number = 5;
    
    
    [[NSUserDefaults standardUserDefaults] setInteger:number forKey:@"myKey"];
    
    // This has the error "Expected identifier or '(' before '[' token"
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // This has the error "Expected identifier or '(' before '[' token"
    
    int myInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"myKey"];
    
    // This has the error "Initialize element is not constant"
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4

    That code needs to go inside a method (or two).
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    haha, I couldn't figure it out.

    Yeah, you probably want to stick this all in the - viewDidLoad method
     
  6. Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
  7. timbojill macrumors member

    Joined:
    May 21, 2011
    #7
    Can you please post the code or e-mail it to timbojill@gmail.com. I an working on a app that needs to save a high score.
     
  8. Wellington2k, Aug 21, 2011
    Last edited: Aug 21, 2011

    Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #8
    Alright. Here ya go! I'll give you an example project. View attachment SaveTest.zip
    If you use it in your game don't forget to give credit!

    By the way how do you save multiple integers?
    Like one is for the best score and one is for the worst.
     
  9. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    You're asking credit for something someone else on this forum wrote for you? kinda bad.
    But, to save multiples, just add another integer to another key called "worstScore" and "HighestScore", don't see the issue here..
    It's a dictionary, if you don't know what it is, back to the documentation with you.
    So you can save keys, and assign values to it.
     
  10. Wellington2k, Aug 22, 2011
    Last edited: Aug 23, 2011

    Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #10
    I meant to say give credit to the person how showed how to save and the score adding, subtracting, and reseting code to me. Sorry I wasn't specific enough.

    Never mind I got it. I had to add one to the loaded integer instead of the variable itself.

    Thank you all!
     

Share This Page