0
我在網上搜索了很長時間。但沒用。請幫助或嘗試提供一些想法如何實現這一點。存檔和取消存檔自定義類(有一個屬性作爲對象數組),總是失敗
當解除存檔應用程序崩潰,我得到這個消息:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason:
'*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (_TtCC11ArchiveTest8dogHouse3dog)
for key (NS.objects); the class may be defined in source code or a library that is not linked'
這是什麼意思? 任何人都可以幫忙?提前致謝。
現在,我認識到終止,因爲其他代碼,就像這樣: enter image description here
,如果我嘗試: enter image description here 它做得很好,買爲何在viewDidLoad中的代碼寫做別的哪種方法聯繫起來?類?
我有一個類:
let ModelPath = "Model.plist".cacheDir()
class dogHouse: NSObject , NSCoding{
var dogs:[Dog]?
required init(coder aDecoder: NSCoder)
{
dogs = aDecoder.decodeObject(forKey: "dogs") as? [Dog]
super.init()
}
func encode(with aCoder: NSCoder) {
if dogs != nil{
aCoder.encode(dogs, forKey: "dogs")
}
}
class Dog: NSObject , NSCoding {
var name : String?
required init(coder aDecoder: NSCoder)
{
name = aDecoder.decodeObject(forKey: "name") as! String?
super.init()
}
func encode(with aCoder: NSCoder) {
if name != nil{
aCoder.encode(name, forKey: "name")
}
}
//ArchiveModels
func saveModel() -> Bool{
return NSKeyedArchiver.archiveRootObject(self, toFile: ICModelPath)
}
//UnArchiveModels
class func loadArchiver() -> [Dog]?{
let obj = NSKeyedUnarchiver.unarchiveObject(withFile: ICModelPath)
if obj != nil {
print(obj.debugDescription)
let model = obj as? dogHouse
return model?.dogs
}
return nil
}
}