gpt4 book ai didi

swift - RealmSwift 也需要 Realm

转载 作者:行者123 更新时间:2023-11-28 06:20:53 25 4
gpt4 key购买 nike

我正在将一个项目迁移到 Swift 3,并且在使用 RealmSwift (2.6.1) 和 Genome (3.2.0) 时遇到了一些问题。我在 Xcode 中遇到 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 之外,这还需要导入 Realm,并且当我的类被初始化时,它试图使用 RLMRealm 而不是 Realm。警告说“必需的”初始值设定项“init(realm:schema:)”必须由“Object”的子类提供,但必需的 init 使用 RLMRealm 而不是 Realm 有什么建议吗?

我也在使用 Genome,它需要这个初始化,这就是为什么 Realm 首先要求初始化器:

required convenience init(node: Node, in context: Context) throws {
self.init()
}

所以 init 看起来像这样:

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(使用相应的 Swift 2.3 版本的 Realm 和 Genome)中一切正常,但现在它不工作了。

整个模型:

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:),只定义方便需要的init(node :in:) 像下面这样抛出

class BaseModel : RealmSwift.Object, MappableBase, Identifiable {

convenience required init(node: Node, in context: Context) throws {
try self.init(node: node, in: context)
}

...

}

关于swift - RealmSwift 也需要 Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com