iPhone:将布尔值保存到核心数据中
我已将核心数据属性之一设置为布尔值。现在,我需要设置它,但 XCode 不断告诉我它可能不会响应 setUseGPS。
[ride setUseGPS: useGPS.on];
在核心数据中设置布尔值的方法是什么?我的所有其他属性都是这样设置的,而且效果很好。那么,不确定为什么布尔值不能以这种方式设置?
I have set up one of my core data attributes as a Boolean. Now, I need to set it, but XCode keeps telling me that it may not respond to setUseGPS.
[ride setUseGPS: useGPS.on];
What is the method for setting a boolean in core data? All my other attributes are set this way, and they work great. So, not sure why a boolean does not work to be set this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Core Data“没有”布尔类型(有,但它是一个 NSNumber)。
所以设置相当于useGPS = YES。
反之亦然:
更新:
正如 SKG 所指出的,使用 Objetive-C 中的文字,您现在可以以更简单的方式做到这一点:
Core Data "does not have" a Boolean type (it does, but it is an NSNumber).
So to set the equivalent of useGPS = YES.
And the other way around:
Update:
As pointed out by SKG, With literals in Objetive-C you can now do it in a simpler way:
作为已接受答案的替代方法,您可以简单地将托管对象接口定义中的类型从 NSNumber* 更改为 BOOL,例如:
讨论了各种替代方法 此处,但是 克里斯·汉森的回答对我来说最具启发性,尤其是:
为了更一致的 Cocoa 实施:
谢谢克里斯,希望这对某人有帮助。
As an alternative approach to the accepted answer, you can simply change the typing from an NSNumber* to a BOOL in the managed object interface definition, such as:
Various alternative approaches are discussed here, but Chris Hanson's response was most illuminating for me, especially:
and for a more aligned Cocoa implementation:
Thanks Chris, and hope that this helps someone.
只是为了补充 @RickiG 答案,在 Swift 中从
Bool
创建NSNumber
的方法,反之亦然(至少从 v4.2 开始)是:Just to complement @RickiG answer, the way to create a
NSNumber
from aBool
and vice-versa in Swift (at least since v4.2) is:对此的“修复”(恕我直言,这是 Apple SDK 中的一个错误)是将以下代码添加到 CoreData 生成的类中。注意:如果您在类别中的单独文件中执行此操作,则每次在 Xcode 中重新生成 CoreData 类时都不必重新复制/粘贴它
The "fix" for this (IMHO, it's a bug in Apple's SDK) is to add the following code to your CoreData-generated class. NB: if you do this in a category, in a separate file, then you don't have to re-copy/paste it each time you regenerate the CoreData classes inside Xcode