iPhone:将布尔值保存到核心数据中

发布于 2024-09-03 11:11:01 字数 193 浏览 11 评论 0原文

我已将核心数据属性之一设置为布尔值。现在,我需要设置它,但 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 技术交流群。

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

发布评论

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

评论(4

薄荷梦 2024-09-10 11:11:01

Core Data“没有”布尔类型(有,但它是一个 NSNumber)。

所以设置相当于useGPS = YES。

[entity setUseGPS:[NSNumber numberWithBool:YES]];

反之亦然:

BOOL isGPSOn = [[entity useGPS] boolValue];

更新:
正如 SKG 所指出的,使用 Objetive-C 中的文字,您现在可以以更简单的方式做到这一点:

[entity setUseGPS:@YES];

BOOL isGPSOn = entity.useGPS.boolValue;

Core Data "does not have" a Boolean type (it does, but it is an NSNumber).

So to set the equivalent of useGPS = YES.

[entity setUseGPS:[NSNumber numberWithBool:YES]];

And the other way around:

BOOL isGPSOn = [[entity useGPS] boolValue];

Update:
As pointed out by SKG, With literals in Objetive-C you can now do it in a simpler way:

[entity setUseGPS:@YES];

BOOL isGPSOn = entity.useGPS.boolValue;
凝望流年 2024-09-10 11:11:01

作为已接受答案的替代方法,您可以简单地将托管对象接口定义中的类型从 NSNumber* 更改为 BOOL,例如:

@property (nonatomic) BOOL useGPS;   // Notice that the 'retain' is also removed as we're now dealing with a scalar rather than an NSObject

讨论了各种替代方法 此处,但是 克里斯·汉森的回答对我来说最具启发性,尤其是:

如果您有数字属性
(包括布尔属性)
必需的,您可以将其键入为
标量代替,核心数据就可以了
正确的事情:

@property(非原子)BOOL isDone;

即使该属性是可选的,
那仍然有效 - 它只是
将“不存在”与“错误”混为一谈。

为了更一致的 Cocoa 实施:

您可能想做的另一件事
将属性命名为“完成”并且只是
将 getter 指定为“isDone”。那是
通常的 Cocoa 命名约定:

@property(非原子,getter=isDone)
BOOL 完成;

然后你可以写“if (item.done) {
... }" 或 "item.done = NO;" 以及
编译器仍会生成 -isDone
用于访问该属性。

谢谢克里斯,希望这对某人有帮助。

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:

@property (nonatomic) BOOL useGPS;   // Notice that the 'retain' is also removed as we're now dealing with a scalar rather than an NSObject

Various alternative approaches are discussed here, but Chris Hanson's response was most illuminating for me, especially:

If you have a numeric attribute
(including a boolean attribute) that's
required, you can just type it as a
scalar instead, and Core Data will do
the right thing:

@property (nonatomic) BOOL isDone;

Even if the attribute is optional,
that'll still work - it'll just
conflate "not present" with "false."

and for a more aligned Cocoa implementation:

One other thing you might want to do
is name the property "done" and just
specify the getter as "isDone." That's
the usual Cocoa naming convention:

@property (nonatomic, getter=isDone)
BOOL done;

Then you can write "if (item.done) {
... }" or "item.done = NO;" and the
compiler will still generate -isDone
for accesses of the property.

Thanks Chris, and hope that this helps someone.

蛮可爱 2024-09-10 11:11:01

只是为了补充 @RickiG 答案,在 Swift 中从 Bool 创建 NSNumber 的方法,反之亦然(至少从 v4.2 开始)是:

let nsNumberFromBool = NSNumber(booleanLiteral: true) // or false
let boolFromNSNumber = nsNumberFromBool.boolValue

Just to complement @RickiG answer, the way to create a NSNumber from a Booland vice-versa in Swift (at least since v4.2) is:

let nsNumberFromBool = NSNumber(booleanLiteral: true) // or false
let boolFromNSNumber = nsNumberFromBool.boolValue
薆情海 2024-09-10 11:11:01

对此的“修复”(恕我直言,这是 Apple SDK 中的一个错误)是将以下代码添加到 CoreData 生成的类中。注意:如果您在类别中的单独文件中执行此操作,则每次在 Xcode 中重新生成 CoreData 类时都不必重新复制/粘贴它

- (BOOL)useGPS
{
    [self willAccessValueForKey:@"useGPS"];
    BOOL myuseGPS = [[self primitiveUseGPS] boolValue];
    [self didAccessValueForKey:@"useGPS"];
    return myuseGPS;
}

- (void)setUseGPS:(BOOL)newValue
{
    [self willChangeValueForKey:@"useGPS"];
    [self setPrimitiveUseGPS:[NSNumber numberWithBool:newValue]];
    [self didChangeValueForKey:@"useGPS"];
}

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

- (BOOL)useGPS
{
    [self willAccessValueForKey:@"useGPS"];
    BOOL myuseGPS = [[self primitiveUseGPS] boolValue];
    [self didAccessValueForKey:@"useGPS"];
    return myuseGPS;
}

- (void)setUseGPS:(BOOL)newValue
{
    [self willChangeValueForKey:@"useGPS"];
    [self setPrimitiveUseGPS:[NSNumber numberWithBool:newValue]];
    [self didChangeValueForKey:@"useGPS"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文