Search code examples
nsuserdefaultsswift3xcode8ios10

Save dictionary in userdefaults in swift 3 with xcode 8


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 is a dictionary and I am running on the simulator.

For some reason this is not saving the value and 'nil' is printed. Wondering if it's a simulator problem.


Solution

  • This problem seems to be caused by having two versions of xcode/simulator installed.

    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.

    After following these steps the simulator is able to save to UserDefaults.