加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 可选Int在领域

发布时间:2020-12-14 05:40:52 所属栏目:百科 来源:网络整理
导读:我想在国内使用一个可选的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不支
我想在国内使用一个可选的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?不管用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读