gpt4 book ai didi

swift - Realm :Swift `let` 属性不能标记为动态

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:16 24 4
gpt4 key购买 nike

我正在使用 Xcode 7.2、Swift 2.1.1。我下面有一个 Realm 模型对象

class B: Object {
dynamic let lists = List<A>()
}

但是 Swift 编译器给我一个错误提示:

Property cannot be marked as dynamic because its type cannot be represented in Objective-C

我看到 Realm 的文档说:

Realm model properties need the dynamic var attribute in order for these properties to become accessors for the underlying database data.

There are two exceptions to this: List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective-C runtime, which is used for dynamic dispatch of dynamic properties, and should always be declared with let

但是现在声明 let 似乎并不能解决这个问题。我错过了什么?

最佳答案

您引用的文档包括以下内容(强调我的):

List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective-C runtime, […], and should always be declared with let.

这意味着您的属性(property)应该这样声明:

let lists = List<A>()

Realm Swift 文档最近获得了 property declaration cheatsheet希望能阐明不同类型声明的要求。

关于swift - Realm :Swift `let` 属性不能标记为动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824078/

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