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.
For Swift 3
UserDefaults.standard.setValue(token, forKey: "user_auth_token")
print("\(UserDefaults.standard.value(forKey: "user_auth_token")!)")
Working perfectly here with this..!
let dict:[String:String] = ["key":"Hello"]
UserDefaults.standard.set(dict, forKey: "dict")
let result = UserDefaults.standard.value(forKey: "dict")
print(result!)
// Output -> { key:hello;}