RealmSwift也要求Realm也
问题描述:
我正在将一个项目迁移到Swift 3,并遇到了RealmSwift(2.6.1)和Genome(3.2.0)的一些问题。我得到在Xcode是说我需要这些inits的境界错误:RealmSwift也要求Realm也
required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
self.init(realm: realm, schema: schema)
}
required convenience init(value: Any, schema: RLMSchema) {
self.init(value: value, schema: schema)
}
然而,这需要除进口境界到RealmSwift,当我的类初始化它试图使用RLMRealm代替领域。警告说'必需的'初始化'init(realm:schema :)'必须由'Object'的子类提供,但是那需要init使用RLMRealm而不是Realm任何建议?
我使用的基因组,以及需要这个初始化,这就是为什么境界是要求摆在首位初始化:
required convenience init(node: Node, in context: Context) throws {
self.init()
}
所以inits看起来像这样一起:
class BaseModel : RealmSwift.Object, MappableBase, Identifiable {
required init() {
super.init()
}
required convenience init(node: Node, in context: Context) throws {
self.init()
}
required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
self.init(realm: realm, schema: schema)
}
required convenience init(value: Any, schema: RLMSchema) {
self.init(value: value, schema: schema)
}
在Swift 2.3(使用Realm和Genome的相应Swift 2.3版本)中,没有任何这些初始化器都能正常工作,但现在它不起作用。
整个模型:
import RealmSwift
import Genome
import Realm
protocol Identifiable {
var identifier: String { get set }
}
class BaseModel : RealmSwift.Object, MappableBase, Identifiable {
required init() {
super.init()
}
required convenience init(node: Node, in context: Context) throws {
try self.init(node: node, in: context)
}
required convenience init(realm: RLMRealm, schema: RLMObjectSchema) {
self.init(realm: realm, schema: schema)
}
required convenience init(value: Any, schema: RLMSchema) {
self.init(value: value, schema: schema)
}
dynamic var identifier = ""
dynamic var updatedAt: Date?
override static func primaryKey() -> String {
return "identifier"
}
static func newInstance(_ node: Node, context: Context = EmptyNode) throws -> Self {
let map = Map(node: node, in: context)
let new = self.init()
try new.sequence(map)
return new
}
func sequence(_ map: Map) throws {
switch map.type {
case .fromNode:
if self.identifier.isEmpty {
// only map id if there isn't one, otherwise Realm complains about modified primaryKey
try self.identifier <~ map["id"]
}
updatedAt = Date()
case .toNode:
if !self.identifier.isEmpty {
try self.identifier ~> map["id"]
}
}
}
func objectRepresentation() -> [String : AnyObject] {
if let result = try? self.toObject() {
return result as? [String : AnyObject] ?? [:]
} else {
return [:]
}
}
static func objectInRealm(_ realm: Realm, identifier: String?) -> Self? {
if let identifier = identifier {
return realm.object(ofType: self, forPrimaryKey: identifier)
} else {
return nil
}
}
static func createOrFindObject(inRealm realm: Realm, identifier: String) -> Self {
if let foundObject = realm.object(ofType: self, forPrimaryKey: identifier) {
return foundObject
} else {
return realm.create(self, value: ["identifier" : identifier], update: false)
}
}
}
答
你并不需要重写init(realm:schema:)
和init(realm:schema:)
,只是定义convenience required init(node:in:) throws
像下面这样。
class BaseModel : RealmSwift.Object, MappableBase, Identifiable {
convenience required init(node: Node, in context: Context) throws {
try self.init(node: node, in: context)
}
...
}
您是否碰巧在类中声明了任何非可选的,未初始化的属性? – NRitH
不,我检查过,但已经很好的想法。 – charliework
通常情况下,只有当您的class_中的某些东西需要初始化时,需要重写所需的初始化器,如属性。 – NRitH