本文介绍了使用xcode 8将字典保存在swift 3中的userdefaults中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过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中的userdefaults中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:59