swift2 如何使用NSUserDefaults在Swift中存储自定义类的数组?

axkjgtzd  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(171)

我有一个名为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()

我已经看过X1E0F1X和X1E1F1X,但我没有运气,我发现它很难理解。
当我只是尝试保存到NSUserDefaults时,系统会告诉我以下信息:

Attempt to set a non-property-list object (
    "PersonApp.Person"
) as an NSUserDefaults/CFPreferences value for key personArray

有人能够帮助我通过NSUserDefaults实际保存自定义对象(自定义词典)数组吗?

3pvhb19x

3pvhb19x1#

您的Person类应如下所示:

雨燕三号

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)")

雨燕四号

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");
    }
}
hjqgdpho

hjqgdpho2#

您需要实现NSCoding协议来将自定义对象存储到NSUserDefaults中。

jjjwad0x

jjjwad0x3#

雨燕4

  • 向您的班级确认可编码协议
  • 使用JsonEncoder类序列化(数据)对象
  • 现在将数据保存到userDefaults

看这个答案就明白了Save Array of Custom Swift object to userDefaults

相关问题