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


当前回答

使用下面的代码。

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

其他回答

对于希望找到存储在模拟器中的首选项的新位置的用户。 这是我找到存储值的地方。

/Users/vikas/Library/Developer/CoreSimulator/Devices/CE5A0444-BD98-4FEE-A839-92728D6E9895/data/Containers/Data/Application/F7430839-ED2C-408A-8A8E-FE7FFAABA8E2/Library/Preferences

因为我们可以看到有很大的标识符,很难从终端猜出来,所以我建议在终端或finder中搜索它们。

文件名应该以{bundle id}结尾。Plist,例如com.sellerbuddy.online.plist,这样你就可以直接进入终端,输入你的应用程序包标识符。

find ~/Library/Developer/CoreSimulator/Devices -type f -name "com.sellerbuddy.online.plist"

对于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编辑器中打开它,并浏览您想要的持久值。

斯威夫特3

print(UserDefaults.standard.dictionaryRepresentation())

在模拟器中运行时,我有时会使用下面的代码片段打印出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

我希望对于实际的设备也是如此。