Posted By

espinallab on 02/04/10


Tagged

NSUserDefaults


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
bobbym245
cnathan


Saving and Loading NSUserDefaults


 / Published in: Objective C
 

URL: http://iphone.zcentric.com/?p=147

  1. This will go over a quick run through of using NSUserDefaults. This is the class to use to save user preferences for example. It is pretty straight forward.
  2. Saving Data
  3.  
  4. This is how you save data.
  5.  
  6. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  7. // saving a string
  8. [prefs setObject:@"test" forKey:@"stringVal"];
  9. // saving a int
  10. [prefs setObject::23 forKey:@"intVal"];
  11. // saving it all
  12. [prefs synchronize];
  13.  
  14. Loading Data
  15.  
  16. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  17. // getting the string
  18. stringVal = [prefs stringForKey:@"stringVal"];
  19. // getting the int
  20. intVal = [prefs integerForKey:@"intVal"];
  21.  
  22. That is it.. pretty simple.

Report this snippet  

You need to login to post a comment.