Where is a Mac Application's NSUserDefaults Data Stored?

I am using NSUserDefaults to store some data in my application.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"dummy string" forKey:@"lastValue"];
[prefs synchronize];

For testing purposes I need to see the System Preferences plist file where my NSUserDefaults data is saving on the Mac.

I know where the iOS application user defaults are stored, but I don't know about mac application. Where is a Mac Application's NSUserDefaults Data Stored?

36267 次浏览

~/Library/Preferences/com.example.myapp.plist

它们可以在不止一个地方找到:

~/Library/Preferences/com.example.myapp.plist
~/Library/SyncedPreferences/com.example.myapp.plist

如果沙箱

~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist
~/Library/Containers/com.example.myapp/Data/Library/SyncedPreferences/com.example.myapp.plist

(Xcode 7.3.1,macOS 10.11.6)

如果您正在使用应用程序组,请参阅“其他”

    if let prefs = NSUserDefaults(suiteName: "group.groupApps")  {
...
}

Plist 文件会在这里:

~/Library/Group Container/Group. groupApps/Library/Preferences/groupp.groupApps.plist

在 Sierra 这里,我找到了数据: ~/Library/Application Support/

One more possible location for these data comes into play when trying things out in a Playground. I was experimenting with UserDefaults in a Playground, using XCode 8.3 and Swift 3, and wanted to see the resulting plist file. After some detective work (UserDefaults files have the bundle identifier in the filename and calling Bundle.main.bundleIdentifier in a Playground gives the XCode identifier) I found to my great surprise that the UserDefaults data was added to:

~/Library/Preferences/com.apple.dt.Xcode

换句话说,键和值被添加到 XCode 首选项文件中!我仔细检查了一遍,找到了一些不太可能的字符串,它们确实被添加到了那里。我没有勇气尝试使用 XCode 已经在使用的一些密钥,但谨慎似乎是好的。