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


当前回答

对于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;
    ...

其他回答

在阅读了这个问题的公认答案后,我把这个简单的脚本放在一起,打开iOS模拟器用来存储NSUserDefaults首选项的plist文件,虽然它假设了某种设置(非常适合我的设置),但它可以作为其他人的起点。

$ cat open-prefs-plist.sh
#!/bin/sh

# The project name based on the workspace path, e.g. "MyProject" from "./MyProject.xcworkspace"
WORKSPACE_NAME=$(echo `find . -name *.xcworkspace -type d -exec basename {} \;` | cut -d'.' -f1)
SIMULATOR_PATH="$HOME/Library/Application Support/iPhone Simulator"
# The App's bundle ID taken from its info plist, e.g "com.myproject" from "./MyProject/MyProject-Info.plist"
BUNDLE_ID=`/usr/libexec/PlistBuddy -c Print:CFBundleIdentifier $WORKSPACE_NAME/$WORKSPACE_NAME"-Info.plist"`
# Open all plist files in the simulator path that match the app's bundle ID 
# normally one per iOS version
find "$SIMULATOR_PATH" -name $BUNDLE_ID".plist" -type f -print0 \
    | while IFS= read -r -d '' PLIST; do
    echo $PLIST
    open "$PLIST"
done

例放置:

$ ls -1
MyProject
MyProject Tests
MyProject.xcodeproj
MyProject.xcworkspace
Podfile
open-prefs-plist.sh

在Swift 4.0中

//func dictionaryRepresentation() -> [String : AnyObject]

因为NSUserDefaults.standardUserDefaults()返回[String: AnyObject]

我们将它转换到NSDictionary中。然后用括号'()'把它括起来将允许我们调用。allkeys或。allvalues就像你在任何NSDictionary上一样

 print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)

它将在更高版本的SWIFT 4上工作

只需要把代码放在AppDelegate的any方法中,并在那里设置断点

**> let path =
> NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
> FileManager.SearchPathDomainMask.userDomainMask, true).first**

当你运行一个项目,打印“路径”,你会得到路径到达信息。使用userdefaults进行Plist 然后到finder,粘贴这个路径 那份文件找到你了

在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

Swift 5 Xcode 11.2解决方案

这是您的UserDefaults键值将在plist文件中的整个路径。跟踪并找到你的应用程序包标识符。plist文件。

/Users/'您的用户名'/Library/Developer/CoreSimulator/Devices/4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2/data/Containers/ data/ Application/56D7CE31-9A8B-4371-9B0F-9604E239423B0/Library/Preferences

这里的“4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2”是您的设备ID

“56D7CE31-9A8B-4371-9B0F-9604E239423B0”是您的申请ID

我通常通过在查找器中根据最近修改的日期对文件夹进行排序来获得它们。最近编辑的文件夹是我的设备ID和应用ID。

享受吧!