いくつかのオブジェクトを配列に保存しようとしていますが、オブジェクトは継承されたプロパティを使用していますが、オブジェクトを読み込もうとすると、
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")
}