有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。
当前回答
在Swift中,我们可以使用以下功能:-
斯威夫特3。X & 4.x
获取所有键和值:
for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
print("\(key) = \(value) \n")
}
检索用户默认值的完整字典表示:
print(Array(UserDefaults.standard.dictionaryRepresentation()))
检索密钥:
// Using dump since the keys are an array of strings.
dump(Array(UserDefaults.standard.dictionaryRepresentation().keys))
检索值:
我们也可以在这里使用dump,但这将返回values数组中每个元素的完整继承层次结构。如果需要关于对象的更多信息,则使用dump,否则使用正常的print语句。
// dump(Array(UserDefaults.standard.dictionaryRepresentation().values))
print(Array(UserDefaults.standard.dictionaryRepresentation().values))
快2.倍
检索用户默认值的完整字典表示:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())
检索密钥:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array)
检索值:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().values.array)
其他回答
我在桌面上保留了模拟器文件夹的快捷方式,在那里保存应用程序,即:
/Users/gary/Library/Application Support/iPhone Simulator/User/Applications
按最近的日期排序,然后进入最近的应用程序文件夹库/首选项,并在plist编辑器中查看文件。
在Swift 2.2中
let path = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let folder = path[0]
NSLog("Your NSUserDefaults are stored in this folder: \(folder)/Preferences")
将打印出NSUserDefaults在Xcode调试控制台的plist文件夹位置。复制路径字符串。打开Finder,选择Go菜单项中的“转到文件夹”,粘贴路径字符串。双击plist文件。你会在Xcode编辑器中看到内容。
只能在模拟器中工作
谢谢@Niels Castle
对于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;
...
使用下面的代码。
NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
模拟器应用程序
这个shell脚本搜索应用程序的名称,获取bundle id,并打开包含Plist文件的文件夹。
#!/bin/bash
appname="$1"
[ -z $appname ] && read -p "Application name : " appname
apppath=$(find ~/Library/Developer/CoreSimulator/Devices/ -name "$appname.app" -print -quit)
if [[ ! -z $apppath ]]; then
appbundle=$(osascript -e "id of app \"$apppath\"")
find ~/Library/Developer/CoreSimulator/Devices/ -name "$appbundle.plist" -exec bash -c 'open "$(dirname "$1")"' -- {} \;
else
echo "No application found by that name: $appname.app"
fi
扩展脚本版本
用法:iphone- App -文件夹“My App”
#!/bin/bash
appname="$1"
[ -z "$appname" ] && read -p "Application name : " appname
apppath=$(find ~/Library/Developer/CoreSimulator/Devices -name "$appname.app" -print -quit)
if [[ ! -z $apppath ]]; then
appbundle=$(osascript -e "id of app \"$apppath\"")
echo "Found app $appname (${appbundle})"
echo -e "\033[1;30m$apppath\033[0m"
plists=$(find ~/Library/Developer/CoreSimulator/Devices -name "$appbundle.plist" -print -quit)
count=$(echo plists | wc -l | sed "s/ //g")
if [[ $count -eq 1 ]] && [[ -f "$plists" ]]; then
echo -e "\033[1;32mUserDefaults found for $appname\033[0m"
echo -e "\033[1;30m$plists\033[0m"
plistutil -i "$plists"
/usr/bin/open $(dirname "$plists")
elif [[ ${#plists} -gt 0 ]]; then
echo -e "\033[1;32mUserDefaults found for $appname\033[0m"
i=1
while read line; do
echo "[${i}] ${line} "
i=$((i+1))
done < <(echo "$plists")
echo
read -p "Select defaults to read: [1-${count}] " choice
plist=$(echo ${plists} | sed -n "${choice}p")
plistutil -i "$plist"
/usr/bin/open $(dirname "$plist")
else
echo -e "\033[31mNo UserDefaults plist found for $appname\033[0m"
fi
else
echo -e "\033[31mNo application found by that name: $appname.app\033[0m"
fi
发现应用My app (com.organisation.MyApp) /用户/组织/图书馆/开发/ CoreSimulator /设备/ 3 e4c8dc3 - 6 - ff4 - 428 f - a624 b78dbe0b8c83 /数据/集装箱/包/应用程序/ 960 a5232 - 219 d - 4 - c46 - 8 - ca3 App.app - 01 e259d8ddad / 为我的应用程序找到UserDefaults
Mac应用程序
defaults read com.bundleid.app
推荐文章
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何在Mac OS安装时停止MySQL ?
- 如何解散ViewController在Swift?
- 保存字符串到NSUserDefaults?
- PIP成功安装包,但在命令行中找不到可执行文件
- 什么是NSLocalizedString等效在Swift?
- 无法在Mac OS X 10.9上安装Lxml
- 在OS X上使用sed进行就地编辑
- MacVim和普通Vim有什么区别?
- 查看保存的NSUserDefaults的简单方法?
- 如何创建一个字符串的格式?
- 如何添加UITableViewCell之间的间距
- 电话:用于文本输入的数字键盘
- 在为设备编译时,Apple Mach-O连接器错误