Core Data mishandles NSCFBoolean?

2019-05-31 01:08发布

问题:

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.