我有一个名为Person
的自定义类,可以在输入信息时存储有关某人的各种属性。
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
}
}
创建字典后,将其放入数组中。点击按钮时,我在将数组保存到NSUserDefaults
时遇到问题。
personArray.append(newPerson) // newPerson = dictionary of attributes
NSUserDefaults.standardUserDefaults().setObject(personArray, forKey: "personArray")
NSUserDefaults.standardUserDefaults().synchronize()
我看过How to store custom objects in NSUserDefaults和Saving custom SWIFT class with NSCoding to UserDefaults,但我没有运气,发现很难理解。
当我只是尝试保存到NSUserDefaults时,我被告知以下内容:
Attempt to set a non-property-list object (
"PersonApp.Person"
) as an NSUserDefaults/CFPreferences value for key personArray
有人能通过NSUserDefaults帮我实际保存一系列自定义对象(自定义词典)吗?
答案 0 :(得分:19)
您的Person类应如下所示:
斯威夫特3:
class Person : NSObject, NSCoding{
// 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
}
required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObjectForKey("name") as? String;
self.age = aDecoder.decodeObjectForKey("age") as? String;
self.html_url = aDecoder.decodeObjectForKey("html") as? String;
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.name, forKey: "name");
aCoder.encodeObject(self.age, forKey: "age");
aCoder.encodeObject(self.html_url, forKey: "html");
}
}
这里有一个从NSUserDefaults保存和检索数组的例子:
let p = Person()
p.name = "person1"
p.age = "12"
p.html_url = "www.google.ro"
let p2 = Person()
p2.name = "person2"
p2.age = "11"
p2.html_url = "www.google.ro"
let array = [p, p2]
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(NSKeyedArchiver.archivedDataWithRootObject(array), forKey: "persons")
userDefaults.synchronize()
let array : [Person]
array = NSKeyedUnarchiver.unarchiveObjectWithData(userDefaults.objectForKey("persons") as! NSData) as! [Person]
print("\(array[0].name)\(array[1].name)")
Swift 4:
class Person : NSObject, NSCoding{
// 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
}
required init?(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as? String;
self.age = aDecoder.decodeObject(forKey: "age") as? String;
self.html_url = aDecoder.decodeObject(forKey: "html") as? String;
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name");
aCoder.encode(self.age, forKey: "age");
aCoder.encode(self.html_url, forKey: "html");
}
}
答案 1 :(得分:5)
您需要实施NSCoding
协议才能将自定义对象存储到NSUserDefaults
中。请查看此示例http://nshipster.com/nscoding/
答案 2 :(得分:2)
Swift 4
请参阅此答案以获得清晰的想法 Save Array of Custom Swift object to userDefaults