核心数据错误处理NSCFBoolean? [英] Core Data mishandles NSCFBoolean?

查看:153
本文介绍了核心数据错误处理NSCFBoolean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据管理有一个布尔类型属性的对象。

I've got a Core Data managed object that has an attribute with a "Boolean" type.

在我的头文件我有这样的:
@属性(非原子,保留)的NSNumber * includeInHistory;

In my header file I've got this: @property (nonatomic, retain) NSNumber * includeInHistory;

和我使用的是@dynamic includeInHistory实施

and I'm using a @dynamic includeInHistory implementation

在保存到磁盘之前,该管理对象的实例进行交互,我已经得到的东西,实际上通过NSNumber的接口映射到NSCFBoolean。我使用JSON框架,以EN code含有从核心数据值来一些字典。

When interacting with an instance of this managed object before saving to disk, I've got something that actually maps to a NSCFBoolean through the NSNumber interface. I'm using "json-framework" to encode some dictionary containing values coming from Core Data.

问题是,保存和检索回数据后,includeInHistory返回这似乎是一个标准的NSNumber(整数,而不是类型为布尔值)。这是有问题的转换成JSON,当它映射到includeInHistory:1,而不是includeInHistory:真正的

The problem is that after saving and retrieving the data back, includeInHistory returns what appears to be a standard NSNumber (integer, not typed as Boolean). This is problematic as when converted to JSON it maps to "includeInHistory" : 1 instead of includeInHistory" : true

现在,我不得不求助于拆箱,然后reboxing每次我都快要导出为JSON,但是这似乎是对我的一个错误....我在这里失去了一些东西?

For now, I've had to resort to unboxing, then reboxing everytime I'm about to export as JSON, but this seems like a bug to me.... Am I missing something here ?

感谢

推荐答案

至于核心数据而言布尔只是一个NSNumber的,其值限制为0或1。核心数据预计接收控制器和/或视图code到正常显示。

As far as Core Data is concerned a Boolean is just a NSNumber with its values limited to 0 or 1. Core Data expects the receiving controller and/or view code to display it properly.

不过,既然你都继承 NSManagedObject 反正,那么我建议写一个includeInHistoryValue方法,可以自动做装箱和拆箱。那么你的JSON解析器会处理原始布尔吧。

However, since you are subclassing NSManagedObject anyway, then I would suggest writing a includeInHistoryValue methods that do the boxing and unboxing automatically. Then your JSON parser will deal with the primitive boolean instead.

您可能还需要考虑使用mogenerator因为这将增加这些装箱和拆箱的方法和维护您的子类为你自动。

You might also want to consider using mogenerator as that will add those boxing and unboxing methods and maintain your subclasses for you, automatically.

这篇关于核心数据错误处理NSCFBoolean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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