斯威夫特2 Xcoode 7.3存儲陣列自定義類的嵌套自定義類來standardUserDefaults
我嘗試存儲這樣的:
var someArray = [Class1(id: 1, titel: "Titel 2", something: Class2(somevar: 20))]
與NSUserDefaults的:
let arrayData = NSKeyedArchiver.archivedDataWithRootObject(someArray) NSUserDefaults.standardUserDefaults().setObject(arrayData, forKey: "array")
我的課程如下所示:
class Class1 {
var id: Int
var titel: String!
var something: Class2
init(id: Int, titel: String, something: Class2) {
self.id = id
self.titel = titel
self.something = something
}
required init(coder aDecoder: NSCoder) {
self.id = aDecoder.decodeIntForKey("id")
self.titel = aDecoder.decodeStringForKey("titel")
???
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(id, forKey: "id")
aCoder.encodeObject(titel, forKey: "titel")
???
}
class Class2: Class1
{
var somevar: Int
init(setback: Int) {
self.somevar = somevar
}
}
我需要在這些類中添加什麼?
(不得不添加一些莫爾細節,但我認爲它是自我解釋)