有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。
当前回答
在Swift 4.0中
//func dictionaryRepresentation() -> [String : AnyObject]
因为NSUserDefaults.standardUserDefaults()返回[String: AnyObject]
我们将它转换到NSDictionary中。然后用括号'()'把它括起来将允许我们调用。allkeys或。allvalues就像你在任何NSDictionary上一样
print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
其他回答
在模拟器中运行时,我有时会使用下面的代码片段打印出NSUserDefaults文件的位置
NSArray *path = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); NSString *folder = [path objectAtIndex:0]; NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);
它生成首选项文件夹的路径
你的NSUserDefaults保存在这个文件夹:/Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences
您的NSUserDefaults文件位于首选项文件夹中,并根据您的前缀和应用程序名称命名。
dk.castleandersen.dreamteam.grid.plist
我希望对于实际的设备也是如此。
你可以打印当前所有的NSUserDefaults到日志:
键:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
键和值:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
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。
享受吧!
在Swift 2.2中
let path = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let folder = path[0]
NSLog("Your NSUserDefaults are stored in this folder: \(folder)/Preferences")
将打印出NSUserDefaults在Xcode调试控制台的plist文件夹位置。复制路径字符串。打开Finder,选择Go菜单项中的“转到文件夹”,粘贴路径字符串。双击plist文件。你会在Xcode编辑器中看到内容。
只能在模拟器中工作
谢谢@Niels Castle
在Swift 4.0中
//func dictionaryRepresentation() -> [String : AnyObject]
因为NSUserDefaults.standardUserDefaults()返回[String: AnyObject]
我们将它转换到NSDictionary中。然后用括号'()'把它括起来将允许我们调用。allkeys或。allvalues就像你在任何NSDictionary上一样
print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
推荐文章
- 为什么在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是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”