Swift 3保存并从userDefaults中检索自定义对象

我在Playground中使用Swift 3,Xcode 8.0:

import Foundation class Person: NSObject, NSCoding { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } required convenience init(coder aDecoder: NSCoder) { let name = aDecoder.decodeObject(forKey: "name") as! String let age = aDecoder.decodeObject(forKey: "age") as! Int self.init( name: name, age: age ) } func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: "name") aCoder.encode(age, forKey: "age") } } 

创buildPerson的数组

 let newPerson = Person(name: "Joe", age: 10) var people = [Person]() people.append(newPerson) 

编码数组

 let encodedData = NSKeyedArchiver.archivedData(withRootObject: people) print("encodedData: \(encodedData))") 

保存到userDefaults

 let userDefaults: UserDefaults = UserDefaults.standard() userDefaults.set(encodedData, forKey: "people") userDefaults.synchronize() 

 print("saved object: \(userDefaults.object(forKey: "people"))") 

从userDefaults中检索

 if let data = userDefaults.object(forKey: "people") { let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) print("myPeopleList: \(myPeopleList)") }else{ print("There is an issue") } 

只需检查存档的数据

 if let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: encodedData){ print("myPeopleList: \(myPeopleList)") }else{ print("There is an issue") } 

我不能正确地将数据对象保存到userDefaults,此外,在底部的检查创build错误“致命错误:意外地发现零,同时展开一个可选值”。 “检查”行也显示保存的对象为零。 这是我的对象的NSCoder错误?

Swift 4注意

您可以再次在Playground中保存/testing您的值


Swift 3

UserDefaults需要在一个真实的项目中testing。 注意:不需要强制同步。 如果要testing游乐场中的编码/解码,可以使用键控存档器将数据保存到文档目录中的plist文件。 你还需要在你的课堂上解决一些问题:

 class Person: NSObject, NSCoding { let name: String let age: Int init(name: String, age: Int) { self.name = name self.age = age } required init(coder decoder: NSCoder) { self.name = decoder.decodeObject(forKey: "name") as? String ?? "" self.age = decoder.decodeInteger(forKey: "age") } func encode(with coder: NSCoder) { coder.encode(name, forKey: "name") coder.encode(age, forKey: "age") } } 

testing:

 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // setting a value for a key let newPerson = Person(name: "Joe", age: 10) var people = [Person]() people.append(newPerson) let encodedData = NSKeyedArchiver.archivedData(withRootObject: people) UserDefaults.standard.set(encodedData, forKey: "people") // retrieving a value for a key if let data = UserDefaults.standard.data(forKey: "people"), let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Person] { myPeopleList.forEach({print( $0.name, $0.age)}) // Joe 10 } else { print("There is an issue") } } } 
 let age = aDecoder.decodeObject(forKey: "age") as! Int 

Swift 3已经改变了; 这不再适用于值types。 现在正确的语法是:

 let age = aDecoder.decodeInteger(forKey: "age") 

有各种不同types的解码…()函数:

 let myBool = aDecoder.decodeBoolean(forKey: "myStoredBool") let myFloat = aDecoder.decodeFloat(forKey: "myStoredFloat") 

编辑: Swift 3中所有可能的decodeXXX函数的完整列表

编辑:

另一个重要的注意事项:如果以前保存的数据是用老版本的Swift编码的,那么这些值必须使用decodeObject()进行解码, 但是一旦使用encode(…)重新编码数据,就不能再如果是值types,则使用decodeObject()进行解码。 因此,Markus Wyss的回答将允许您处理使用Swift版本对数据进行编码的情况:

 self.age = aDecoder.decodeObject(forKey: "age") as? Int ?? aDecoder.decodeInteger(forKey: "age") 

尝试这个:

 self.age = aDecoder.decodeObject(forKey: "age") as? Int ?? aDecoder.decodeInteger(forKey: "age")