领域中的可选Int [英] Optional Int in Realm

查看:83
本文介绍了领域中的可选Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Realm中使用Optional Int,并且出现了我认为的旧错误.

I am trying to use an Optional Int in Realm and am getting an old error I think.

代码

dynamic var reps: Int? = nil

错误

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

我正在将Realm 0.96.1和XCode 7.1一起使用

I am using Realm 0.96.1 with XCode 7.1

我了解到Realm文档中说Int不作为Optional支持,但 https://twitter.com/realm/status/656621989583548416 .那是来自Realm Twitter的,所以这就是为什么我感到困惑的原因.是否支持Optional Int还是仍然不支持?

I understand in the Realm documentation it says the Int isn't supported as an Optional but https://twitter.com/realm/status/656621989583548416. That is from the Realm twitter so thats why I am confused. Are Optional Int supported or still no?

推荐答案

来自Realm文档:

StringNSDateNSData属性声明为可选或非可选属性.

String, NSDate, and NSData properties can be declared as optional or non-optional using the standard Swift syntax.

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

Optional numeric types are declared using 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支持IntFloatDoubleBoolInt的所有大小版本(Int8Int16Int32Int64)

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

更新:

Realm在 Tweet 中提到的可选Int仅仅是关于RealmOptional方式的错误修复用Int

The Optional Ints that were mentioned in the Tweet by Realm were just regarding a bugfix for the RealmOptional way of implementing an Optional numeric value with the sized versions of Int

根据来自Realm的家伙,如果要在领域对象.您不能像其他可选类型一样简单地使用它.

According to the guys from Realm you still have to use RealmOptional if you want to have Optional numeric values in a Realm object. You cannot simply use it like other Optional types.

所以dynamic var reps: Int?将不起作用.

这篇关于领域中的可选Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆