有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。
当前回答
使用下面的代码。
NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
其他回答
对于OS X应用程序,使用默认命令行实用程序更简单,而不是寻找应用程序的默认plist文件。
NAME defaults -- access the Mac OS X user defaults system SYNOPSIS defaults [-currentHost | -host hostname] read [domain [key]] defaults [-currentHost | -host hostname] read-type domain key defaults [-currentHost | -host hostname] write domain { 'plist' | key 'value' } defaults [-currentHost | -host hostname] rename domain old_key new_key defaults [-currentHost | -host hostname] delete [domain [key]] defaults [-currentHost | -host hostname] { domains | find word | help } DESCRIPTION defaults allows users to read, write, and delete Mac OS X user defaults from a command-line shell. Mac OS X applications and other programs use the defaults system to record user preferences and other information that must be maintained when the applications aren't running (such as default font for new documents, or the position of an Info panel). Much of this information is accessible through an appli- cation's Preferences panel, but some of it isn't, such as the position of the Info panel. You can access this information with defaults
例子:
$ defaults read com.apple.Safari
{
AutoplayPolicyWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
AutoplayQuirksWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
DefaultBrowserPromptingState2 = 4;
...
你可以在模拟器中找到你的应用的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编辑器中打开它,并浏览您想要的持久值。
您可以检查数组中由返回的每个键的值
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
对于 Xcode 7
NSUserDefaults standardDefaults存储在这里:
用户/用户/{}/图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/数据/应用程序/ {UUID}
一个套件/应用程序组的NSUserDefaults存储在这里:
/用户/ {USER} /图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/共享/ AppGroup / {UUID} / {GROUP_NAME} .plist /图书馆/偏好
我建议使用https://github.com/scinfu/NCSimulatorPlugin,因为现在所有东西都隐藏在uuid后面,很难找到。它允许轻松访问您的模拟器应用程序目录。
Swift 5 Xcode 11.2解决方案
这是您的UserDefaults键值将在plist文件中的整个路径。跟踪并找到你的应用程序包标识符。plist文件。
/Users/'您的用户名'/Library/Developer/CoreSimulator/Devices/4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2/data/Containers/ data/ Application/56D7CE31-9A8B-4371-9B0F-9604E239423B0/Library/Preferences
这里的“4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2”是您的设备ID
“56D7CE31-9A8B-4371-9B0F-9604E239423B0”是您的申请ID
我通常通过在查找器中根据最近修改的日期对文件夹进行排序来获得它们。最近编辑的文件夹是我的设备ID和应用ID。
享受吧!
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- “你有邮件”的消息在终端,os X
- 为什么ARC仍然需要@autoreleasepool ?
- Mac OS X中的环境变量
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从macOS完全卸载蟒蛇
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?