My Issue:
- 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()
. - 昨天,我更新了我的领域框架,从0.91.5到0.92.0。我发现Realm团队已经将Swift部分和Objective-C部分从之前的整个Cocoa框架中分离出来,团队也改变了语法。我已经将代码更正为最新的领域语法,但是我仍然在init()中遇到一些麻烦。
The Error:
- The Compiler thrown the error:
fatal error: use of unimplemented initializer init(realm:schema:) for CardModel
. - 编译器抛出错误:致命错误:对CardModel使用未实现的初始化程序init(realm:schema:)。
- 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. - 我使用了project的MultiPeer Connectivity framework,这意味着我需要对数据进行编码和解码。
- I tried to change or add other
init()
toCardModel
, but it did not work. - 我试图更改或向CardModel添加其他的init(),但它不起作用。
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
伊桑乔
3 个解决方案
#1
15
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()
基本上,您不应该创建“init”方法,但是您可以创建“方便的init”方法。在这种情况下,你不能调用super.init()但你调用的是self。init()
so in your case above you have to remove override init() and the other init can be:
在上面的例子中,你必须删除重写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
}
More information: https://github.com/realm/realm-cocoa/issues/1849
更多信息:https://github.com/realm/realm-cocoa/issues/1849。
#2
3
You need to implement the init like that:
你需要像这样实现init:
init(object:schema:) {
super.init(object: object, schema: schema)
}
There are various posts on github about that.
关于这一点,github上有很多帖子。
#3
1
I ended up needing to add:
最后我需要补充:
required convenience init?(_ map: Map) {
self.init()
}
#1
15
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()
基本上,您不应该创建“init”方法,但是您可以创建“方便的init”方法。在这种情况下,你不能调用super.init()但你调用的是self。init()
so in your case above you have to remove override init() and the other init can be:
在上面的例子中,你必须删除重写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
}
More information: https://github.com/realm/realm-cocoa/issues/1849
更多信息:https://github.com/realm/realm-cocoa/issues/1849。
#2
3
You need to implement the init like that:
你需要像这样实现init:
init(object:schema:) {
super.init(object: object, schema: schema)
}
There are various posts on github about that.
关于这一点,github上有很多帖子。
#3
1
I ended up needing to add:
最后我需要补充:
required convenience init?(_ map: Map) {
self.init()
}