核心数据错误处理 NSCFBoolean?

发布于 2024-08-07 00:52:24 字数 493 浏览 5 评论 0原文

我有一个核心数据托管对象,它有一个“布尔”类型的属性。

在我的头文件中我有这个: @property(非原子,保留)NSNumber * includeInHistory;

我正在使用 @dynamic includeInHistory 实现

在保存到磁盘之前与此托管对象的实例进行交互时,我得到了一些实际上通过 NSNumber 接口映射到 NSCFBoolean 的东西。我正在使用“json-framework”对一些包含来自核心数据的值的字典进行编码。

问题是,在保存并检索数据后,includeInHistory 返回看起来是标准 NSNumber 的内容(整数,而不是布尔类型)。这是有问题的,因为当转换为 JSON 时,它映射到 "includeInHistory" : 1 而不是 includeInHistory" : true

现在,我不得不拆箱,然后每次我要导出为 JSON 时重新装箱,但这看起来像对我来说是一个错误......我在这里错过了什么吗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-08-14 00:52:24

就 Core Data 而言,布尔值只是一个 NSNumber,其值限制为 0 或 1。Core Data 期望接收控制器和/或视图代码正确显示它。

但是,由于您无论如何都要子类化 NSManagedObject ,那么我建议编写一个 includeInHistoryValue 方法来自动进行装箱和拆箱。然后你的 JSON 解析器将处理原始布尔值。

您可能还想考虑使用 mogenerator,因为它将添加这些装箱和拆箱方法并自动为您维护子类。

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.

手心的温暖 2024-08-14 00:52:24

听起来 JSON 框架无法识别您的号码是布尔类型。我认为这是一个错误;我建议您举报

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文