我试图将一些对象保存在数组中,但我的对象使用继承的属性但是当试图读取它说的对象时
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -decodeObjectForKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!'
结构是
Service(SuperClass)
Service1(Subclass)
Service2
Service3
以这种方式我使用此代码
var S1:Service1 = Service1()
S1.apiKey = Apikey.text!
TableViewController.agregar(S1)
let datos = NSKeyedArchiver.archivedDataWithRootObject(TableViewController.Servicios!)
NSUserDefaults.standardUserDefaults().setObject(datos, forKey: "ServiciosGuardados")
NSUserDefaults.standardUserDefaults().synchronize()
这是类功能
class Servicio:NSObject, NSCoding{
var nombre:String = "null"
var tipo:Int = 0
override init() {}
required init(coder aDecoder: NSCoder = NSCoder.empty()) {
self.nombre = aDecoder.decodeObjectForKey("nombre") as! String
self.tipo = aDecoder.decodeObjectForKey("tipo") as! Int
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(nombre, forKey: "nombre")
aCoder.encodeObject(tipo, forKey: "tipo")
}
-我的问题出在从 Servicio 继承的 Service1 中
class Service1: Servicio {
var apiKey = "null"
override init() {
super.init(coder: NSCoder())
}
required init(coder aDecoder: NSCoder) {
super.init(coder: NSCoder())
if let apiKey = aDecoder.decodeObjectForKey("apiKey") as? String {
self.apiKey = apiKey
}
}
override func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.apiKey, forKey: "apiKey")
}