我正在网上搜索很长时间。但是没用。请帮助或尝试给出一些想法来实现这一目标。
当取消存档的应用崩溃时,我收到以下消息:
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
}
}
最佳答案
也许您错过了init
函数中的参数?
required init(coder aDecoder: NSCoder)
{
dogs = aDecoder.decodeObject(forKey: "dogs") as? [Dog]
super.init(aDecoder) //HERE
}
关于swift - 存档和取消存档自定义类(具有作为对象数组的属性),总是失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41820500/