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


当前回答

你可以打印出应用程序的首选项目录的路径:didFinishLaunchingWithOptions:回调在你的AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print(FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first!)
    return true
}

然后你可以直接查看plist文件,看看里面保存了什么。

其他回答

在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)

在Swift 4.0中

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

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

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

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

在阅读了这个问题的公认答案后,我把这个简单的脚本放在一起,打开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

MacOS应用 进入:/Users/{User}/Library/Containers/com。{你的公司}。{your app}/Data/Library/Preferences,然后用Xcode打开你的app的pList。

对于 Xcode 7

NSUserDefaults standardDefaults存储在这里:

用户/用户/{}/图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/数据/应用程序/ {UUID}

一个套件/应用程序组的NSUserDefaults存储在这里:

/用户/ {USER} /图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/共享/ AppGroup / {UUID} / {GROUP_NAME} .plist /图书馆/偏好

我建议使用https://github.com/scinfu/NCSimulatorPlugin,因为现在所有东西都隐藏在uuid后面,很难找到。它允许轻松访问您的模拟器应用程序目录。