本文介绍了致命错误:使用未实现的初始化程序"init(realm:schema :)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 昨天,我为我的项目(用Swift编写)将
Realm
框架从0.91.5
更新为了0.92.0
.我发现Realm团队已经将Swift
部分和Objective-C
部分与之前的整个Cocoa Framework分开了,该团队还更改了语法.而且我已经将代码更正为最新的Realm语法,但是init()
仍然存在一些问题.
- Yesterday, I updated my
Realm
framework from0.91.5
to0.92.0
for my project (written in Swift). I found that the Realm Team had already separatedSwift
part andObjective-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 withinit()
.
- 编译器抛出错误:
fatal error: use of unimplemented initializer init(realm:schema:) for CardModel
. - 问题是以前版本的
Realm
并未发生此错误. - 我将
MultiPeer Connectivity
框架用于项目,这意味着我需要Encode
和Decode
进行数据交换. - 我尝试将其他
init()
更改或添加到CardModel
,但是没有用.
- 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 toEncode
andDecode
for exchanging of data. - I tried to change or add other
init()
toCardModel
, but it did not work.
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
Ethan Joe
推荐答案
前几天,我遇到了相同的问题:
I ran in the same problem the other day:
基本上,您不应该创建"init"方法,但是可以创建"convenience init"方法.在这种情况下,您无法调用super.init(),但会调用诸如self.init()
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()
因此,在上述情况下,您必须删除重写init(),而另一个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
}
更多信息:> https://github.com/realm/realm-cocoa/issues/1849
这篇关于致命错误:使用未实现的初始化程序"init(realm:schema :)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!