NSUserDefaults to save and retrieve the data in iPhone

N

In many applications we need to save the state of the application, in that case we can use NSUserDefaults.

Lets start with saving the data into NSUserDefaults. We can save small amount of data in NSUserDefaults such as username, high scrore, etc.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:@”username” forKey:@”UserName”];

This is to save the NSString. Also integer, float, double can be saved.

e. g. [defaults setInteger:100 forKey:@”HighScore”];

Now how to retrieve the data by using the keys which we have assigned…

NSString *myUserName = [defaults stringForKey:@”UserName”];

int  highScore = [defaults integerForKey:@”HighScore”];

Thats it…

Happy coding………!!!:)

About the author

minakshi.bhosale
By minakshi.bhosale

Category