ios 如何在 Swift for Realm 模型中设置主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26152254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:57:48  来源:igfitidea点击:

How to set primary key in Swift for Realm model

iosswiftrealm

提问by jeffjv

I'm using Realm in a new iOS Swift project. I'm using Xcode 6.0.1 with iOS SDK 8.0 and Realm 0.85.0

我在一个新的 iOS Swift 项目中使用 Realm。我将 Xcode 6.0.1 与 iOS SDK 8.0 和 Realm 0.85.0 一起使用

I'm trying to use the new Realm primary key feature so I can do an addOrUpdateObject.

我正在尝试使用新的 Realm 主键功能,以便我可以执行addOrUpdateObject.

Here is a sample model:

这是一个示例模型:

import Foundation
import Realm

class Foo: RLMObject {
    dynamic var id = 0
    dynamic var title = ""

    func primaryKey() -> Int {
        return id
    }
}

And how I'm trying to add/update a new object:

以及我如何尝试添加/更新新对象:

let foo = Foo()
foo.title = titleField.text
foo.id = 1

// Get the default Realm
let realm = RLMRealm.defaultRealm()

// Add to the Realm inside a transaction
realm.beginWriteTransaction()
realm.addOrUpdateObject(foo)
realm.commitWriteTransaction()

I get this error:

我收到此错误:

RLMExecption', reason: ''Foo' does not have a primary key and can not be updated

RLMExecption',原因:''Foo' 没有主键,无法更新

Here are the docs on the primary key. I'm probably not setting it correctly: http://realm.io/docs/cocoa/0.85.0/api/Classes/RLMObject.html#//api/name/primaryKey

这是关于主键的文档。我可能没有正确设置它:http: //realm.io/docs/cocoa/0.85.0/api/Classes/RLMObject.html#//api/name/primaryKey

Latest docs are here now: https://realm.io/docs/objc/latest/api/Classes/RLMObject.html#//api/name/primaryKey

最新文档在这里:https: //realm.io/docs/objc/latest/api/Classes/RLMObject.html#//api/name/primaryKey

回答by Thomas Goyne

primaryKeyneeds to be a class function which returns the name of the property which is the primary key, not an instance method which returns the value of the primary key.

primaryKey需要是一个类函数,它返回作为主键的属性的名称,而不是返回主键值的实例方法。

class Foo: RLMObject {
    dynamic var id = 0
    dynamic var title = ""

    override class func primaryKey() -> String? {
        return "id"
    }
}

回答by CherryKuczery

The return type of primaryKey()is optional:

的返回类型primaryKey()是可选的:

class Foo: RLMObject {
    dynamic var id = 0
    dynamic var title = ""

    override class func primaryKey() -> String? {
        return "id"
    }
}

回答by Maria Ortega

For Swift 5:

对于 Swift 5:

import RealmSwift

     class Signature: Object {

           @objc dynamic var id = ""

            override static func primaryKey() -> String? {
                return "id"
            }
      }

To avoid: Terminating app due to uncaught exception 'RLMException', reason: 'Primary key property 'id' does not exist on object.

避免:由于未捕获的异常“RLMException”而终止应用程序,原因:对象上不存在“主键属性”“id”。