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


当前回答

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。

享受吧!

其他回答

斯威夫特3

print(UserDefaults.standard.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。

享受吧!

您可以检查数组中由返回的每个键的值

[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]

我根据Morion的建议构建了这个方法,以便更好地呈现。通过调用[self logAllUserDefaults]使用它

- (void) logAllUserDefaults
{
    NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];
    NSArray *values = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues];
    for (int i = 0; i < keys.count; i++) {
        NSLog(@"%@: %@", [keys objectAtIndex:i], [values objectAtIndex:i]);
    }
}

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

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