ios – Swift 2.0中的动态可选属性

我已经看过这篇文章 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 typesoptional properties的文档.

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

RealmOptional supports Int,Float,Double,Bool,and all of the sized versions of Int (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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...