I have a custom class called Person
that stores various attributes about someone when they enter information.
class Person {
// Person dictionary variable
var name: String?
var age: String?
var html_url: String?
init(json: NSDictionary) { // Dictionary object
self.name = json["name"] as? String
self.age = json["age"] as? String
self.html_url = json["html_url"] as? String // Location of the JSON file
}
}
Once the dictionary is created, it is then placed into an array. I am having problems saving the array into NSUserDefaults
when a button is tapped.
personArray.append(newPerson) // newPerson = dictionary of attributes
NSUserDefaults.standardUserDefaults().setObject(personArray, forKey: "personArray")
NSUserDefaults.standardUserDefaults().synchronize()
I have had a look at How to store custom objects in NSUserDefaults and Saving custom SWIFT class with NSCoding to UserDefaults but I have had no luck and am finding it hard to understand.
I when I simply try to save into NSUserDefaults, I am told the following:
Attempt to set a non-property-list object (
"PersonApp.Person"
) as an NSUserDefaults/CFPreferences value for key personArray
Would anyone be able to help me actually save an array of custom objects (custom dictionaries) via NSUserDefaults?