Realm
框架从0.91.5
更新为0.92.0
。我发现Realm团队已经将Swift
部分和Objective-C
部分与之前的整个Cocoa框架分开,团队也改变了语法。我已经将我的代码更正为最新的Realm语法,但我仍然遇到了init()
的问题。fatal error: use of unimplemented initializer init(realm:schema:) for CardModel
。Realm
未发生此错误。MultiPeer Connectivity
框架用于项目,这意味着我需要Encode
和Decode
来交换数据。init()
到CardModel
,但它不起作用。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")
}
}
请教我如何解决这个问题。
非常感谢您的指导和时间。
Ethan Joe
答案 0 :(得分:15)
前几天我遇到了同样的问题:
基本上你不应该创建" init"方法,但你可以创建"便利init"方法。在这种情况下,你不能调用super.init(),但你调用self.init()
之类的东西所以在上面你的情况下你必须删除覆盖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
}
答案 1 :(得分:3)
你需要像这样实现init:
init(object:schema:) {
super.init(object: object, schema: schema)
}
github about that上有各种帖子。
答案 2 :(得分:1)
我最后需要添加:
required convenience init?(_ map: Map) {
self.init()
}