【问题标题】:Dynamic properties in RealmRealm 中的动态属性
【发布时间】:2016-01-15 14:35:51
【问题描述】:

我开始在 iOS 8 或更高版本上使用 Realm 并查看 Realm 中的文档。我注意到所有属性前面都有dynamic 关键字。在 Realm 中是必需的吗?我已阅读有关关键字的 Apple 文档,可在此处找到。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html

【问题讨论】:

    标签: ios swift2 realm


    【解决方案1】:

    在 Swift 3 中,我们这样声明我们的属性

    dynamic var Name : String = ""
    

    在 Swift 4 中,我们这样声明我们的属性

    @objc dynamic var Name : String = ""
    

    【讨论】:

      【解决方案2】:

      是的,对于普通的var 属性是强制性的。来自realm docs

      领域模型属性需要dynamic var 属性才能使这些属性成为底层数据库数据的访问器。

      对此有两个例外:ListRealmOptional 属性不能声明为动态,因为通用属性不能在用于动态分派动态属性的 Objective-C 运行时中表示,并且应该始终是用let 声明。

      dynamic 关键字允许 Realm 收到模型变量更改的通知,从而将它们反映到数据库中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多