Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSUserDefaults

iCLSn0wiCLSn0w Posts: 34Registered Users
edited February 2012 in Tutorial Discussion
Hello everyone if you 're here , this is because you would use NSUserDefaults,
So it's very simple :
If you want save score :
created a NSInteger for score
@property (nonatomic) IBOutlet NSInteger MainInt;

and write that :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:MainInt forKey:@MainInt];
[prefs synchronize];

for display score in your stats view :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSInteger myInt = [prefs integerForKey:@MainInt];
label.text = [NSString stringWithFormat:@Score:%d,myInt];

it's really simple !!

if you want save another type :
nsstring:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@TextToSave forKey:@keyToLookupString];

double :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setDouble:3.1415 forKey:@doubleKey];

float:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:1.2345678 forKey:@floatKey];

This is suggested to synch prefs, but is not needed
[prefs synchronize];

For display another type :
nsstring:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:@keyToLookupString];

float :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float myFloat = [prefs floatForKey:@floatKey];

now you know how to save (score , string, text, number, anything) with nsuserdefaults.

iCLSn0w
Sign In or Register to comment.