Swift 2.0 中的动态可选属性

Dynamic optional properties in Swift 2.0

我看过这个 post but I don't want to have to wrap up the class in an NSObject. This just is concerning the Realm database I don't have to have nil properties but it would be a nice way I think to model my database. In the Realm documentation which can be found here https://realm.io/docs/swift/latest/ 它说支持可选。这是我的

代码

dynamic var complete: Bool? = nil

这是我的

错误

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

我知道这与上面的 post 的代码和错误相同,但我很好奇 Realm 文档是否说它支持它,他们是否有其他解决方法?

来自 supported types and optional properties 上的文档。

String, NSDate, NSData and Object properties can be optional. Storing optional numbers is done using RealmOptional.

RealmOptional supports Int, Float, Double, Bool, and all of the sized versions of Int (Int8, Int16, Int32, Int64).

因此 StringNSDateNSDataObject 类型都支持可选项,并且使用标准的 swift 语法。

对于其他数字类型(例如Bool),用RealmOptional完成。然后要使用此 RealmOptional 类型的变量,您可以访问它的 value 属性,这是一个代表您的基础值的可选变量。

// definition (defined with let)
let complete = RealmOptional<Bool>()  // defaults to nil
// usage
complete.value = false  // set non-nil value
...
complete.value = nil    // set to nil again