ios 查看保存的 NSUserDefaults 的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1676938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to see saved NSUserDefaults?
提问by Ethan
Is there a way to see what's been saved to NSUserDefaults
directly? I'd like to see if my data saved correctly.
有没有办法直接查看保存的内容NSUserDefaults
?我想看看我的数据是否正确保存。
采纳答案by RedBlueThing
You can find the pList file for your app in the simulator if you go to:
如果您转到以下位置,您可以在模拟器中找到您的应用程序的 pList 文件:
/users/your user name/Library/Application Support/iPhone Simulator/<Sim Version>/Applications
/users/您的用户名/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:
这个目录有一堆 GUID 命名的目录。如果您正在开发一些应用程序,就会有一些应用程序。所以你需要找到你的应用程序二进制文件:
find . -name foo.app
./1BAB4C83-8E7E-4671-AC36-6043F8A9BFA7/foo.app
Then go to the Library/Preferences directory in the GUID directory. So:
然后转到 GUID 目录中的 Library/Preferences 目录。所以:
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.
在 pList 编辑器中打开它并浏览持久化的值到你的心的内容。
回答by marshn
You can print all current NSUserDefaults to the log:
您可以将所有当前 NSUserDefaults 打印到日志:
Just keys:
只是键:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
Keys and values:
键和值:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
回答by footyapps27
In Swift we can use the following:-
在 Swift 中,我们可以使用以下内容:-
Swift 3.x & 4.x
斯威夫特 3.x 和 4.x
For getting all keys & values:
获取所有键和值:
for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
print("\(key) = \(value) \n")
}
For retrieving the complete dictionary representation of user defaults:
用于检索用户默认值的完整字典表示:
print(Array(UserDefaults.standard.dictionaryRepresentation()))
For retrieving the keys:
用于检索密钥:
// Using dump since the keys are an array of strings.
dump(Array(UserDefaults.standard.dictionaryRepresentation().keys))
For retrieving the values:
用于检索值:
We can use dump here as well, but that will return the complete inheritance hierarchy of each element in the values array. If more information about the objects is required, then use dump, else go ahead with the normal print statement.
我们也可以在这里使用 dump,但这将返回 values 数组中每个元素的完整继承层次结构。如果需要有关对象的更多信息,请使用转储,否则继续使用正常的打印语句。
// dump(Array(UserDefaults.standard.dictionaryRepresentation().values))
print(Array(UserDefaults.standard.dictionaryRepresentation().values))
Swift 2.x
斯威夫特 2.x
For retrieving the complete dictionary representation of user defaults:
用于检索用户默认值的完整字典表示:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())
For retrieving the keys:
用于检索密钥:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array)
For retrieving the values:
用于检索值:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().values.array)
回答by Morion
You can check the values for each key in the array, returned by
您可以检查数组中每个键的值,由返回
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
回答by Niels Castle
I sometimes use the following snippet to print out the location of my NSUserDefaults file when running in the simulator
在模拟器中运行时,我有时会使用以下代码段打印出我的 NSUserDefaults 文件的位置
NSArray *path = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); NSString *folder = [path objectAtIndex:0]; NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);
It yields the path to the preferences folder
它产生首选项文件夹的路径
Your NSUserDefaults are stored in this folder: /Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences
您的 NSUserDefaults 存储在此文件夹中:/Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences
Your NSUserDefaults file is located in the preferences folder and named according to your prefix and appliation name e.g.
您的 NSUserDefaults 文件位于首选项文件夹中,并根据您的前缀和应用程序名称命名,例如
dk.castleandersen.dreamteam.grid.plist
I expect the same to be true for the actual device.
我希望实际设备也是如此。
回答by Hlung
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.
很简单,因为 plist 文件名是<app-bundle-identifier>.plist
,你可以使用find
命令来找到它的路径。但是如果你搜索整台计算机需要很长时间,所以你必须选择一个好的范围,比如~/Library/Developer/CoreSimulator
Xcode 6。
example:
例子:
find ~/Library/Developer/CoreSimulator -type f -name com.awesome.app.plist
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
/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.
从那里你可以使用open
命令。或者,如果您使用iTerm2,只需在路径上单击命令即可打开它。
回答by RyanTCB
In Swift 4.0
在 Swift 4.0 中
//func dictionaryRepresentation() -> [String : AnyObject]
because dictionaryRepresentation of NSUserDefaults.standardUserDefaults() returns [String : AnyObject]
因为 NSUserDefaults.standardUserDefaults() 的 dictionaryRepresentation 返回[String : AnyObject]
We cast it into an NSDictionary
. Then by surrounding it in parenthesis '()' will allow us to to call .allKeys or .allValues just as you would on any NSDictionary
我们将其转换为NSDictionary
. 然后将它括在括号 '()' 中将允许我们像在任何 NSDictionary 上一样调用 .allKeys 或 .allValues
print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
回答by Mr.Javed Multani
Use below code.
使用下面的代码。
NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
回答by weevilgenius
For OS X applications, instead of finding the application's defaults plist file, it is simpler to use the defaults
command line utility.
对于OS X 应用程序,使用defaults
命令行实用程序比查找应用程序的默认 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 withdefaults
姓名
defaults -- access the Mac OS X user defaults system
概要
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 }
描述
defaults
允许用户从命令行 shell 读取、写入和删除 Mac OS X 用户默认值。Mac OS X 应用程序和其他程序使用默认系统来记录用户首选项和其他在应用程序未运行时必须维护的信息(例如新文档的默认字体或信息面板的位置)。大部分信息都可以通过应用程序的首选项面板访问,但有些则不能,例如信息面板的位置。您可以通过以下方式访问此信息defaults
Example:
例子:
$ defaults read com.apple.Safari
{
AutoplayPolicyWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
AutoplayQuirksWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
DefaultBrowserPromptingState2 = 4;
...
回答by user1270061
For Xcode 7
对于 Xcode 7
NSUserDefaults standardDefaultsare stored here:
NSUserDefaults标准默认值存储在这里:
/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Data/Application/{UUID}
/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Data/Application/{UUID}
NSUserDefaults for a suite/app groupare stored here:
套件/应用程序组的NSUserDefaults存储在这里:
/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Shared/AppGroup/{UUID}/Library/Preferences/{GROUP_NAME}.plist
/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/NCSimulatorPluginbecause these days everything is behind UUIDs and are a pain to find. It allows easy access to your simulator app directories.
我建议使用https://github.com/scinfu/NCSimulatorPlugin因为现在一切都在 UUID 后面,很难找到。它允许轻松访问您的模拟器应用程序目录。