Here I will show you how to use NSUserDefaults to save data.
This method is very simple but very slow, so it’s not good to use it to storage a big data.
Just add this line to setup :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
And to read data use:
int value;
value = [prefs intForKey:@”value”];
To write data:
int value = 1;
[prefs setInteger:value forKey:@”value”];
[prefs synchronize];
Remember to synchronize after all saves, not at the end.
Of course you can write/read other data types.
From Mac Dev Center:
– setBool:forKey:
– setFloat:forKey:
– setInteger:forKey:
– setObject:forKey:
– setDouble:forKey:
– setURL:forKey:
In my apps I use it to check whether the app runs at the first time or not.
Just do something like this:
if ([prefs boolForKey:@”first_time”] == 0)
{
// First run
[prefs setBool:1 forKey:@”first_time”];
[prefs synchronize];
}
else
{
// Second run}
It works becouse all unsaved data are from default 0.
Excellent Thank you so much!!!
By: Rushil on October 14, 2010
at 12:34 am
excellent work man
By: Bharath on April 10, 2012
at 6:48 am
You saved my day. Thx so much.
Sitting here close to midnight and found your script. Woooooeeee. 🙂
By: AlGustavson on December 11, 2012
at 11:12 pm
Thanks for helping !
By: brunohdcBruno on February 27, 2013
at 8:06 am