Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 5.0 NSUserdefaults still get stored preferences after deleted the plist

I got a problem about programming an osx application on Xcode 5 while using the NSUserDefaults. Usually, we just use [[NSUserDefaults standardUserDefaults] setObject:@"This is an object" forKey:@"Test"] to remember a user preference. After that, the application will generate a plist file at ~/Library/Preferences/application.bundle.identifier.plist.

The problem is, after I deleted the plist file, the application could still get the preferences I stored. There is no way to clear that plist even if I tried to clean project, restart xcode, delete the files in derived folder. The only way for me to solve this problem is to restart the system, so I guess there is something stored in the memory. The question is how can I clear these stored preferences? (I don't think it's convenient to clear the preferences by adding code manually in debugging and testing.) And I tried the former version of Xcode 4.x, there's no such problem. Anyone has interest could just create a new cocoa project, and add the code like:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    
[defaults setObject:@"This is an object." forKey:@"Test"];     
NSLog(@"%@", [defaults objectForKey:@"Test"]); 

under "applicationDidFinishLaunching". Then go and delete ~/Library/Preferences/application.bundle.identifier.plist. After that, comment the line: [defaults setObject:@"This is an object." forKey:@"Test"]; in your code and run the application again. The console will still show "This is an object." My environment is Mavericks GM and Xcode 5.0(5a1413).

Hope this is not something just only happened to me and appreciated any help!

like image 951
john Avatar asked Oct 21 '13 01:10

john


1 Answers

This is an OS X issue not directly related to the version of Xcode you are using. Apple's official line is that deleting the plist file to remove preferences has never been officially supported, and in more recent OS X releases it is unreliable due to changes in the way the preferences are stored.

The supported way to remove preferences is to use the defaults command at the terminal, e.g.:

defaults delete application.bundle.identifier

The defaults command can also remove/change individual settings with in the preferences. For full details see man defaults.

like image 71
CRD Avatar answered Oct 24 '22 18:10

CRD