Core Data mishandles NSCFBoolean?

风流意气都作罢 提交于 2019-12-08 05:28:00

问题


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

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

and I'm using a @dynamic includeInHistory implementation

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.

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

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 ?

Thanks


回答1:


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.

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.

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




回答2:


Sounds like the JSON Framework isn't recognizing that your number is of Boolean type. I would consider that a bug; I suggest you report it.



来源:https://stackoverflow.com/questions/1492726/core-data-mishandles-nscfboolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!