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

ⅰ亾dé卋堺 提交于 2019-12-08 15:52:00

问题


My Issue:

  • Yesterday, I updated my Realm framework from 0.91.5 to 0.92.0 for my project (written in Swift). I found that the Realm Team had already separated Swift part and Objective-C part from the previous entire Cocoa Framework, the team also change the syntax. And I already corrected my code as the latest Realm syntax, but I still got some trouble with init().

The Error:

  • The Compiler thrown the error: fatal error: use of unimplemented initializer init(realm:schema:) for CardModel.
  • The thing is this error did not occur with the previous version of Realm.
  • I used MultiPeer Connectivity framework for project, which means I need to Encode and Decode for exchanging of data.
  • I tried to change or add other init() to CardModel, but it did not work.

My Code:

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")
  }  
}


Please teach me how to solve this problem.

A big appreciation for your guide and time.

Ethan Joe


回答1:


I ran in the same problem the other day:

Basically you should not create "init" methods but you can create "convenience init" methods. In that case you can't call super.init() but you call something like self.init()

so in your case above you have to remove override init() and the other init can be:

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
}

More information: https://github.com/realm/realm-cocoa/issues/1849




回答2:


You need to implement the init like that:

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

There are various posts on github about that.




回答3:


I ended up needing to add:

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


来源:https://stackoverflow.com/questions/30090603/fatal-error-use-of-unimplemented-initializer-initrealmschema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!