Fatal Error: use of unimplemented initializer 'init(realm:schema:)'

Fatal Error: use of unimplemented initializer 'init(realm:schema:)'

我的问题:

错误:

我的代码:

import RealmSwift

class CardModel: Object {
dynamic var cardID: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var userImage = NSData()
dynamic var status: String = ""
dynamic var cardType: Int = 1
dynamic var cardDate = NSDate()

override init() {
    super.init()
}

init(coder aDecoder: NSCoder) {
    super.init()
    self.userImage = aDecoder.decodeObjectForKey("userImage") as! NSData
    self.cardID = aDecoder.decodeObjectForKey("cardID") as! String
    self.firstName = aDecoder.decodeObjectForKey("firstName") as! String
    self.lastName = aDecoder.decodeObjectForKey("lastName") as! String
    self.status = aDecoder.decodeObjectForKey("status") as! String
    self.cardType = aDecoder.decodeObjectForKey("cardType") as! Int
    self.cardDate = aDecoder.decodeObjectForKey("cardDate") as! NSDate
}

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(self.userImage, forKey: "userImage")
    aCoder.encodeObject(self.cardID, forKey: "cardID")
    aCoder.encodeObject(self.firstName, forKey: "firstName")
    aCoder.encodeObject(self.lastName, forKey: "lastName")
    aCoder.encodeObject(self.status, forKey: "status")
    aCoder.encodeObject(self.cardType, forKey: "cardType")
    aCoder.encodeObject(self.cardDate, forKey: "cardDate")
  }  
}


请教我如何解决这个问题。

非常感谢您的指导和时间。

伊桑·乔

你需要这样实现初始化:

init(object:schema:) {   
    super.init(object: object, schema: schema) 
} 

github about that 上有各种帖子。

前几天我运行遇到了同样的问题:

基本上您不应创建 "init" 方法,但您可以创建 "convenience init" 方法。在那种情况下,你不能调用 super.init() 但你可以调用 self.init()

所以在你上面的例子中,你必须删除 override init() 而另一个 init 可以是:

convenience required init(coder aDecoder: NSCoder) {
    self.init()
    self.userImage = aDecoder.decodeObjectForKey("userImage") as! NSData
    self.cardID = aDecoder.decodeObjectForKey("cardID") as! String
    self.firstName = aDecoder.decodeObjectForKey("firstName") as! String
    self.lastName = aDecoder.decodeObjectForKey("lastName") as! String
    self.status = aDecoder.decodeObjectForKey("status") as! String
    self.cardType = aDecoder.decodeObjectForKey("cardType") as! Int
    self.cardDate = aDecoder.decodeObjectForKey("cardDate") as! NSDate
}

更多信息:https://github.com/realm/realm-cocoa/issues/1849

我最终需要添加:

required convenience init?(_ map: Map) {
    self.init()
}