6

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.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

1 Answers1

3

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];

Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • what do you mean by "not able to enter the path"? – Bryan Chen Feb 25 '13 at 08:35
  • Without seeing the folder you can not enter inside the folder from Finder – Anoop Vaidya Feb 25 '13 at 08:44
  • Thanks for your reply! :-) **One more question/clarification :** I surely *can* see hidden files and navigate pretty much anywhere. However, the `my-app-name.plist` file in the above location (`~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist`) contains *only* values I've set using `setObject:forKey:` and not the ones via `registerDefaults:`. I'm pretty sure the values *are* set (I've tested them - after registration and they are there), however they're *not* in the `.plist`. How's that? – Dr.Kameleon Feb 25 '13 at 08:53
  • 1
    @Dr.Kameleon: sorry, I don't know the answer, but i guess if it doesn't get save there. If my be saved somewhere else path, application package or (JUst a wild guess) :( – Anoop Vaidya Feb 25 '13 at 09:06
  • @AnoopVaidya OK, just figured it out (thanks to : http://stackoverflow.com/questions/2076816/how-to-register-user-defaults-using-nsuserdefaults-without-overwriting-existing/2078651#2078651). `The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.` So everything makes sense : things are stored in the `.plist` *only* if they differ from the defaults registered at launch. – Dr.Kameleon Feb 25 '13 at 09:35
  • Also +1 for the above comment. I've been programming for more-or-less 18 years and "I don't know the answer" is one of the phrases I (almost) never hear from programmers. (Although for me, it's an indication that you *know*, even if not this exact thing...) Cheers! – Dr.Kameleon Feb 25 '13 at 09:45
  • 1
    If you have seen my profile, I have around 3 yrs of cocoa experience and every day I learn many things from SO. thanks to guys like you :) – Anoop Vaidya Feb 25 '13 at 09:48