Links: iPhone Dev Help Forums | Live Chat | iPhone OS Programming Guide | Apple Developer Site

Sunday, April 13, 2008

NSUserDefaults how I love thee!

I'm finding more and more gems in iPhone development that make me really happy. My recent find is NSUserDefaults. Coming from a J2ME and Windows Mobile development background things like NSUserDefaults make me miss J2ME and WinMobile less and less.

NSUserDefaults is a very easy way to store application specific defaults and settings. You instantiate NSUsersDefaults, and then just start saving your records in a Key-Value pair. Depending on the domain you choose to save your defaults NSUsersDefaults will either store your defaults on the filesystem or in memory.

Here's how you use it...

Instantiate NSUsersDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Save your defaults:
[defaults setObject:@"felix" forKey:@"Username"];

Get them back later:
[[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];

That's all to it! 

Now I bet all you J2ME developers are thinking "So what?! J2ME has RecordStore class!" RecordStore is similiar, but doesn't work nearly as nice as NSUserDefaults. NSUserDefaults also has a lot more capability than what I talked about here. Check out the documentation and you'll see what I mean.

1 Comments:

At April 14, 2008 10:02 AM , Blogger Robert said...

Thanks, that's really cool.

 

Post a Comment

<< Home