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


当前回答

你可以打印出应用程序的首选项目录的路径: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编辑器中打开它,并浏览您想要的持久值。

MacOS应用 进入:/Users/{User}/Library/Containers/com。{你的公司}。{your app}/Data/Library/Preferences,然后用Xcode打开你的app的pList。

您可以使用它来获得用户首选项、缓存和许多其他数据的完整路径

print (NSSearchPathForDirectoriesInDomains(。文件目录。

我根据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]);
    }
}

斯威夫特3

print(UserDefaults.standard.dictionaryRepresentation())