有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。


当前回答

对于 Xcode 7

NSUserDefaults standardDefaults存储在这里:

用户/用户/{}/图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/数据/应用程序/ {UUID}

一个套件/应用程序组的NSUserDefaults存储在这里:

/用户/ {USER} /图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/共享/ AppGroup / {UUID} / {GROUP_NAME} .plist /图书馆/偏好

我建议使用https://github.com/scinfu/NCSimulatorPlugin,因为现在所有东西都隐藏在uuid后面,很难找到。它允许轻松访问您的模拟器应用程序目录。

其他回答

你可以NSLog你设置的每个值,比如:

NSLog(@"%@",[[NSUserDefaults standardDefaults] stringForKey:@"WhateverTheKeyYouSet"]);

你可以打印出应用程序的首选项目录的路径:didFinishLaunchingWithOptions:回调在你的AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print(FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first!)
    return true
}

然后你可以直接查看plist文件,看看里面保存了什么。

你可以在模拟器中找到你的应用的pList文件,如果你去:

/users/你的用户名/库/应用程序支持/iPhone模拟器/<Sim版本>/应用程序

这个目录有很多以GUID命名的目录。如果你正在开发一些应用程序,就会有一些这样的应用程序。所以你需要找到你的应用二进制:

find . -name foo.app
./1BAB4C83-8E7E-4671-AC36-6043F8A9BFA7/foo.app

然后进入GUID目录下的Library/Preferences目录。所以:

cd 1BAB4C83-8E7E-4671-AC35-6043F8A9BFA7/Library/Preferences

你应该会找到这样一个文件:

<Bundle Identifier>.foo.pList

在pList编辑器中打开它,并浏览您想要的持久值。

在Swift 4.0中

//func dictionaryRepresentation() -> [String : AnyObject]

因为NSUserDefaults.standardUserDefaults()返回[String: AnyObject]

我们将它转换到NSDictionary中。然后用括号'()'把它括起来将允许我们调用。allkeys或。allvalues就像你在任何NSDictionary上一样

 print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)

很简单,因为plist文件名为<app-bundle-identifier>。Plist,你可以使用find命令找到它的路径。但如果你搜索整台电脑,会花很长时间,所以你必须选择一个好的范围,比如Xcode 6的~/Library/Developer/CoreSimulator。

例子:

查找~/Library/Developer/CoreSimulator -type f -name com.awesome.app.plist

输出将是这样的…

\用户hlung / Library /开发商CoreSimulator / / B61913F6-7D7C-4E45-AE2F-F45220A71823带给您Containers /数据/应用程序/ E4CC51CF-11E5-4168-8A74-6BAE3B89998F / Library / Preferences / com . awesome app。plist

然后你可以使用open命令。或者如果使用iTerm2,只需在路径上单击命令打开它。