我已经看过这篇文章
Optional dynamic properties in Swift,但我不想在NSObject中包装该类.这只是关于Realm数据库我没有nil属性,但我认为这是一个很好的方式来建模我的数据库.在可以在
https://realm.io/docs/swift/latest/中找到的Realm文档中,它表示支持选项.这是我的
码
dynamic var complete: Bool? = nil
这是我的
错误
Property cannot be marked dynamic because its type cannot be represented in Objective-C
我知道这是与上面的帖子相同的代码和错误,但我很好奇,如果Realm文档说它支持它,他们还有另一种解决方法吗?
解决方法
来自
supported types和
optional properties的文档.
String,NSDate,NSDataand Object properties can be optional. Storing optional numbers is done using 07002.
RealmOptionalsupportsInt,Float,Double,Bool,and all of the sized versions ofInt(Int8,Int16,Int32,Int64).
因此,使用标准的swift语法很好地支持String,NSDate,NSData和Object类型的选项.
对于使用RealmOptional完成的其他数字类型(例如Bool).然后,要使用此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