Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Issues/Questions regarding NSUserDefaults

OK, I admit NSUserDefaults, being a Mac-specific thing (and me being a Mac programmer for the last couple of years), is one of the things I haven't delved into that much... so here are some issues/questions I've come across :

I understand that NSUserDefaults is basically an NSMutableDictionary written as a .plist.

My questions :

  1. Given that I'm running OS X 10.7 (Lion) and having enabled Sandbox, where is my app's .plist file? (I've search in both ~/Library/Preferences/com.example.myapp.plist and ~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist but none of these seems valid

  2. I understand that this .plist file is created the first time the app launches, correct?

  3. registerDefaults: is to be used at application launch (e.g. in awakeFromNib) and provide a Dictionary of default values that are immediately stored in the .plist file, and changed only if a different value is set at some point, correct?

  4. When we're setting a specific Key-Value pair, is that pair automatically and immediately saved to the .plist file? And if so, why/when should we use synchronize? (Is using it every single time some value is set an overkill, or should it be reserved for special cases?)


Sidenote : I hope nobody complains about my use of the osx tag. However, I'm really tired of seeing Cocoa Touch / iOS related answers to my (mostly) OSX-related questions. So, here you are... :-)


EDIT : For some really helpful insight on the subject, please have a look at the accepted answer as well as the comments below it.

Upvotes: 6

Views: 237

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

Answer 1. The home directory is hidden in Lion, so you are not able to enter the path(Without seeing the folder you can not enter inside the folder from Finder). You can use Path Finder to move around your hidden directories.

Answer 2. Not always. There can be multiple plists in a single application. Few gets created at first launch, few at some specific action. Actually it depends when the plist file are created and how to use it.

Answer 3. registerDefaults: registers default values are when the user has not set other values to that property. It will not override whatever the user has stored when the app is opened later. You can use it in anywhere, but as stated it will be used once.

Answer 4. For Mac OSX application there is no performance and overkill issues, however for iOS it has. It is always better to use [[NSUserDefaults standardUserDefaults] synchronize];

Upvotes: 3

Related Questions