gpt4 book ai didi

ios - 忽略的属性和简单的 swift 属性

转载 作者:搜寻专家 更新时间:2023-11-01 06:02:21 24 4
gpt4 key购买 nike

使用忽略的属性和只使用简单的 swift 属性在行为上有什么区别?

我需要向不应保存到数据库的 Realm 对象添加属性。 Realm 提供了“忽略的属性”,但如果我只是用普通的 swift 样式编写属性,它不应该保存到 DB。行为上有区别吗?

// Ignored propery
class Person: Object {
dynamic var tmpID = 0

override static func ignoredProperties() -> [String] {
return ["tmpID"]
}
}


// Normal swift property
class Person: Object {
var tmpID = 0 // Realm supported class but with no dynamic
var myClassObj = MyCustomClass() // Class realm does not support
}

最佳答案

由于 Realm 框架中使用的是 Objective-C 运行时,因此必须将 Realm 模型中的 var 属性标记为 dynamic。如果您不将属性标记为 dynamic,它们将不会在运行时更新。

但是,ignoredProperties 是一个完全独立的东西。通过向 ignoredProperties 添加一个属性,您告诉 Realm 不要将该属性保存在数据库中。如果您不将属性标记为 dynamic 并且也不将其添加到 ignoredProperties 中,它仍将保留到 Realm,但不会按预期工作.

如果您需要在您的 Realm 模型类中存储不被持久化的属性,您必须将它们添加到 ignoredProperties

关于ios - 忽略的属性和简单的 swift 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45794016/

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