问题描述
我使用以下代码使用 xcode 8 将对象保存到 UserDefaults(以前是 NSUserDefaults):
I am using the following code to save an object to UserDefaults (previously NSUserDefaults) using xcode 8:
let defaults = UserDefaults.standard()
defaults.set(someObject, forKey: "someObject")
print(defaults.object(forKey: "someObject"))
someObject 是一个字典,我在模拟器上运行.
someObject is a dictionary and I am running on the simulator.
出于某种原因,这并没有保存值,而是打印了nil".想知道是不是模拟器问题.
For some reason this is not saving the value and 'nil' is printed. Wondering if it's a simulator problem.
推荐答案
这个问题好像是安装了两个版本的 xcode/simulator 引起的.
This problem seems to be caused by having two versions of xcode/simulator installed.
对我有用的是卸载 xcode 7 并在我的系统上保留 xcode 8 beta.清空垃圾,重置模拟器并运行.我也重新启动了我的电脑.
What worked for me was uninstalling xcode 7 and just keeping xcode 8 beta on my system. Emptying trash, resetting the simulator and running. I also restarted my computer.
执行这些步骤后,模拟器可以保存到 UserDefaults.
After following these steps the simulator is able to save to UserDefaults.
这篇关于使用 xcode 8 在 swift 3 中的用户默认值中保存字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!