Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Wellington2k

macrumors regular
Original poster
Jun 4, 2011
131
0
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.
 
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!
 
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"
 
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"


That code needs to go inside a method (or two).
 
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.
 
Last edited:
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.
 
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!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.