添加更新为true的对象时的NSUnknownKeyException

时间:2021-05-27 20:11:34

So I have modeled an object as such

所以我已经建模了一个对象

class Post: Object {
    let postId = RealmOptional<Int>()
    dynamic var postImage : PostImage?
    dynamic var postUser: User?

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

and I am trying to persist it like this:

我试图坚持这样:

let realm = try! Realm()
realm.beginWrite()
let post = Post()
post.postId.value = json["image"]["image_id"].intValue
realm.add(post, update: true)
try! realm.commitWrite()

However when the object is a new one and has not been stored yet I get:

但是当对象是一个新对象并且尚未存储但我得到:

** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null).'

**由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类不是密钥的密钥值编码兼容(null)。

There has to be something I am doing wrong because the documentation states that Realms

必须有一些我做错的事情,因为文档说明了Realms

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

will create the object if one with the primary key is not present.

如果没有主键,将创建该对象。

Any help or pointers appreciated!

任何帮助或指针赞赏!

1 个解决方案

#1


1  

You're not doing anything wrong. You found a bug. I filed an issue and have submitted a PR to resolve that.

你没有做错任何事。你发现了一个错误。我提交了一个问题,并提交了一份PR来解决这个问题。

#1


1  

You're not doing anything wrong. You found a bug. I filed an issue and have submitted a PR to resolve that.

你没有做错任何事。你发现了一个错误。我提交了一个问题,并提交了一份PR来解决这个问题。