使用UserDefaults在Swift中保存自定义对象的字典

时间:2017-12-15 22:34:52

标签: swift nsuserdefaults userdefaults

我正在编写一个应用程序来处理联系人并尝试使用UserDefaults保存信息,但我正在获取SIGABRT。这是我的代码,我做错了什么?

class Contact{
var name = String()
var phone = String()

init(name: String, phone: String){
    self.name=name
    self.phone=phone
}
required init(coder decoder: NSCoder){
    self.name = (decoder.decodeObject(forKey: "name") as? String)!
    self.phone = (decoder.decodeObject(forKey: "phone") as? String)!
}
func encode(with coder: NSCoder){
    coder.encode(name, forKey: "name")
    coder.encode(phone, forKey: "phone")
}
}

仅在测试时在ViewDidLoad中创建联系人:

        let userDefaults = UserDefaults.standard
var contactDictionary = Dictionary<String, [Contact]>()

  override func viewDidLoad() {
    super.viewDidLoad()
    contactDictionary["A"] = [Contact(name: "Annabel",phone: "000")]

    let encodedData = NSKeyedArchiver.archivedData(withRootObject: contactDictionary)
    userDefaults.set(encodedData, forKey: "contactDictionary")
    userDefaults.synchronize()
    if let data = userDefaults.data(forKey: "contactDictionary"){
        print("yep")
        contactDictionary = (NSKeyedUnarchiver.unarchiveObject(with: data) as? [String : [Contact]])!
    }
    else{
        print("nope")
    }
}

1 个答案:

答案 0 :(得分:1)

您需要使您的类符合NSCoding并从NSObject继承。将您的联系声明更改为类联系人:NSObject,NSCoding {。顺便说一下,有条不紊地施放以强行拆开它是毫无意义的。 decoder.decodeObject(forKey: "name") as! String

class Contact: NSObject, NSCoding {
    var name = String()
    var phone = String()
    init(name: String, phone: String){
        self.name=name
        self.phone=phone
    }
    required init(coder decoder: NSCoder){
        self.name = decoder.decodeObject(forKey: "name") as! String
        self.phone = decoder.decodeObject(forKey: "phone") as! String
    }
    func encode(with coder: NSCoder){
        coder.encode(name, forKey: "name")
        coder.encode(phone, forKey: "phone")
    }
}

测试:

let contactDictionary = ["A":[Contact(name: "Annabel",phone: "000")]]

let encodedData = NSKeyedArchiver.archivedData(withRootObject: contactDictionary)
UserDefaults.standard.set(encodedData, forKey: "contactDictionary")

if let data = UserDefaults.standard.data(forKey: "contactDictionary") {
    print("yep")
    let contactDictionary2 = NSKeyedUnarchiver.unarchiveObject(with: data) as! [String : [Contact]]
}
else{
    print("nope")
}