Saving and Loading NSUserDefaults


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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.

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.