swift - Realm 方法 addOrUpdateObject() 是否可以从 Swift 调用?

标签 swift realm

根据文档; https://realm.io/docs/objc/latest/api/Classes/RLMRealm.html方法 - (void)addOrUpdateObject:(nonnull RLMObject *)object 可以通过 Obj-C 获得

Swift 文档; https://realm.io/docs/swift/latest/api/Classes/Realm.html 没有提到这个方法。 编辑:有,我一开始没注意到。

在我的 Swift 应用程序中,如果我尝试调用此方法 self.realm!.addOrUpdateObject(model)然后我得到一个编译器错误 Value of type 'Realm' has no member 'addOrUpdateObject'

但我在网上看到其他地方,例如How to properly map JSON properties to model properties in Realm.create人们显然在 Swift 中使用它。

我通过 cocoapods 使用 Realm - pod 'RealmSwift' - 它会关闭 2 个 pod; Realm 和 RealmSwift。我可以在 RLMRealm.mm 中看到这个方法我尝试添加 #import <Realm/Realm.h>到我的桥接头,但这没有用。

如何从 Swift 调用此方法?

最佳答案

这在 Swift 中可用,并且是 defined as :

public func add(object: Object, update: Bool = false)

因此,为了获得与 Obj-C Realm 的方法相同的行为,您可以调用它:

self.realm!.add(model, update: true)

关于swift - Realm 方法 addOrUpdateObject() 是否可以从 Swift 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743999/

相关文章:

ios - 境界 cocoa : finding multiple objects by PKs

ios - 自定义 `UITextField` 子类没有出现在屏幕上

closures - 如何在 swift 中使用 completionBlock?

ios - 无法在 Objective-C View Controller 中使用 Swift 对象的变量

ios - 显示 View Controller 会导致稍后出现问题

swift - RLM 异常 : Object type does not match RLMArray type

android - 当 Observable 在 Schedulers.io() 上订阅时从不正确的线程访问 Realm

json - 在 JsonSerialization 中为特定的 Json 响应转换 JSON 响应的内容

ios - 从不正确的线程 Swift 访问的 Realm

ios - 带有 Realm for WatchOS 的架构 i386 的 undefined symbol