如何在realm.io中处理空值? [英] How to handle null value in realm.io?

查看:77
本文介绍了如何在realm.io中处理空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我所看到的属性值是否为nil的角度来看,它总是引发异常.有时我不能简单地将''空字符串设置为nil,那么领域中的解决方案是什么?谢谢.

from what I see if the value is nil for a property it always throws exception. sometimes i cannot simply set a '' empty string as nil, so what's the solution in realm? thanks.

推荐答案

现在完全支持null.

Null is now fully supported.

旧答案:

路线图上支持nil/NULL. 在此之前,有两种解决方法:

Supporting nil/NULL is on the roadmap. Until then there are two workarounds:

  1. 添加一个单独的属性以指示您的属性是否为空.

  1. Add a separate property to indicate if your property is nil.

@interface IntObject : RLMObject
@property NSInteger myProp;
@property boolean myPropIsNil;
@end

  • 将您的财产包装在一个对象中:

  • Wrap your property in an object:

    对象属性(链接)可以为零.因此,例如,如果您需要一个可为null的int属性,则可以将其包装在新的Realm模型中,如下所示:

    Object properties (links), can be nil though. So if you need a nullable int property, for example, you can wrap it in a new Realm model, like this:

    @interface IntObject : RLMObject
    @property NSInteger myProp;
    @end
    

    然后,只要您想在模型中具有可选的"int"属性,就可以编写以下代码:

    Then anytime you want to have an optional "int" property in your models, you can write this:

    @interface MyModel : RLMObject
    @property IntObject *optionalMyProp;
    @end
    

  • 这篇关于如何在realm.io中处理空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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