如何使用NSUserDefaults在Swift中存储自定义类的数组? [英] How to use NSUserDefaults to store an array of custom classes in Swift?

查看:488
本文介绍了如何使用NSUserDefaults在Swift中存储自定义类的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Person 的自定义类,它在输入信息时存储有关某人的各种属性。

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
    }
}

一旦字典创建后,它将被放入一个数组中。点击按钮时,我在将数组保存到 NSUserDefaults 时遇到问题。

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

我看过如何在NSUserDefaults中存储自定义对象使用NSCoding将自定义SWIFT类保存到UserDefaults 但我没有运气,并且发现它很难理解。

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.

当我只是尝试保存到NSUserDefaults时,我被告知以下内容:

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

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

Would anyone be able to help me actually save an array of custom objects (custom dictionaries) via NSUserDefaults?

推荐答案

您的Person类应如下所示:

Your Person class should look like this:

Swift 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的数组:

And here you have an example of saving and retrieving the array from 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");
    }
}

这篇关于如何使用NSUserDefaults在Swift中存储自定义类的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆