swift – 可选Int在领域

前端之家收集整理的这篇文章主要介绍了swift – 可选Int在领域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在国内使用一个可选的Int,而且我想到了一个旧的错误.

dynamic var reps: Int? = nil

错误

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

我在XCode 7.1中使用Realm 0.96.1

我在领域文档中明白,Int不支持可选,但是https://twitter.com/realm/status/656621989583548416.这是来自领土twitter,所以我为什么感到困惑.是否支持可选内容

从领域文件

String,NSDate和NSData属性可以使用标准Swift语法声明为可选或非可选.

可选数字类型使用RealmOptional声明:

class Person: Object {
    // Optional string property,defaulting to nil
    dynamic var name: String? = nil

    // Optional int property,defaulting to nil
    // RealmOptional properties should always be declared with `let`,// as assigning to them directly will not work as desired
    let age = RealmOptional<Int>()
}

let realm = try! Realm()
try! realm.write() {
    var person = realm.create(Person.self,value: ["Jane",27])
    // Reading from or modifying a `RealmOptional` is done via the `value` property
    person.age.value = 28
}

RealmOptional支持Int,Float,Double,Bool和Int(Int8,Int16,Int32,Int64)的所有大小版本.

更新:

Realm的Tweet中提到的可选Ints只是关于RealmOptional实现一个可选数字值与大小版本的Int的错误修复

如果您想在Realm对象中具有可选数字值,那么According给来自领域的人员仍然需要使用RealmOptional.您不能像其他可选类型一样使用它.

所以动态var reps:Int?不管用.

猜你在找的Swift相关文章