Easy way to see saved NSUserDefaults?

2019-01-02 19:16发布

Is there a way to see what's been saved to NSUserDefaults directly? I'd like to see if my data saved correctly.

19条回答
素衣白纱
2楼-- · 2019-01-02 19:39

You can print all current NSUserDefaults to the log:

Just keys:

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);

Keys and values:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
查看更多
刘海飞了
3楼-- · 2019-01-02 19:39

Easy, since the plist file name is <app-bundle-identifier>.plist, you can use find command to find its path. But it will take very long if you search your whole computer, so you have to pick a good scope, like ~/Library/Developer/CoreSimulator for Xcode 6.

example:

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

the output will be something like this...

/Users/hlung/Library/Developer/CoreSimulator/Devices/B61913F6-7D7C-4E45-AE2F-F45220A71823/data/Containers/Data/Application/E4CC51CF-11E5-4168-8A74-6BAE3B89998F/Library/Preferences/com.awesome.app.plist

And from there you can use open command. Or if you use iTerm2, just command-click on the path to open it.

查看更多
与风俱净
4楼-- · 2019-01-02 19:39

You could NSLog each value you set, like:

NSLog(@"%@",[[NSUserDefaults standardDefaults] stringForKey:@"WhateverTheKeyYouSet"]);
查看更多
情到深处是孤独
5楼-- · 2019-01-02 19:40

You can find the pList file for your app in the simulator if you go to:

/users/your user name/Library/Application Support/iPhone Simulator/<Sim Version>/Applications

This directory has a bunch of GUID named directories. If you are working on a few apps there will be a few of them. So you need to find your app binary:

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

Then go to the Library/Preferences directory in the GUID directory. So:

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

You should find a file that looks like:

<Bundle Identifier>.foo.pList

Open this up in the pList editor and browse persisted values to your heart's content.

查看更多
呛了眼睛熬了心
6楼-- · 2019-01-02 19:40

For Xcode 7

NSUserDefaults standardDefaults are stored here:

/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Data/Application/{UUID}

NSUserDefaults for a suite/app group are stored here:

/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Shared/AppGroup/{UUID}/Library/Preferences/{GROUP_NAME}.plist

I would recommend using https://github.com/scinfu/NCSimulatorPlugin because these days everything is behind UUIDs and are a pain to find. It allows easy access to your simulator app directories.

查看更多
大哥的爱人
7楼-- · 2019-01-02 19:45

Swift 3

print(UserDefaults.standard.dictionaryRepresentation())
查看更多
登录 后发表回答