プロジェクトをSwift 3に移行していて、RealmSwift(2.6.1)とGenome(3.2.0)で問題が発生しました。しかし、それはRealmSwiftに加えてレルムをインポートする必要があり、私のクラスが初期化されるとき、代わりのRLMRealmを使用しようとしている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)
}
:私はINITSこれらを必要と言うXcodeでレルムのためのエラーを取得していますレルム警告は 'required'初期化子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)
}
}
}
クラスに初期化されていない非初期化プロパティを宣言したことはありますか? – NRitH
いいえ、私はすでに良い考え方をチェックしました。 – charliework
通常、必要な初期化子は、プロパティのようにクラス_を初期化する必要があるときにのみオーバーライドする必要があります。 – NRitH