I have a dictionary and I want to save it to NSUserDefaults (or something else so that I can access my variables after I stopped the application), I found an example:
var saved = NSUserDefaults.standardUserDefaults()
let dict = ["Name": "Paul", "Country": "UK"]
saved.setObject(dict, forKey: "SavedDict")
But when I used it for my dictionary, it did not work. (maybe because my dictionary is a little different)
My dictionary is made as follows:
var userDictionary = [Int : Event]()
struct Event {
var sensorName: String
var sensorType: String
var sensorSub: String
}
And I add these elements:
userDictionary[value] = Event(sensorName: "first", sensorType: "Temp", sensorSub: "Third")
And here is what I tried to do to save it.
saved.setObject(userDictionary, forKey: "valueDictionary")
And I get this error:
Cannot convert value of type '[Int: SensorsView.Event]' to expected Type of argument 'AnyObject?'
To avoid this error, I did the following:
self.saved.setObject(self.userDictionary as? AnyObject, forKey: "valueDictionary")
But I can’t get what I saved
, question , , Data ( - ) ,