核心数据和BOOL设置 [英] Core Data and BOOL setup

查看:101
本文介绍了核心数据和BOOL设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序,使用Core Data作为其后端来管理SQLite记录。我有一切使用字符串和数字,但刚刚尝试添加BOOL字段,似乎不能让东西工作。

I am working on an app that uses Core Data as its backend for managing SQLite records. I have everything working with strings and numbers, but have just tried adding BOOL fields and can't seem to get things to work.

在.xcdatamodel,我已经添加一个字段给我的对象叫做 isCurrentlyForSale ,它不是可选的,不是瞬态,而是索引。属性类型设置为布尔值,默认值为NO。

In the .xcdatamodel, I have added a field to my object called isCurrentlyForSale which is not Optional, not Transient, and not Indexed. The attribute's type is set to Boolean with default value NO.

当我从数据模型创建类文件时,为.h头文件中的属性添加的样板代码is:

When I created the class files from the data model, the boilerplate code added for this property in the .h header was:

@property (nonatomic, retain) NSNumber * isCurrentlyForSale;

以及

@dynamic isCurrentlyForSale;

我一直使用布尔作为简单的BOOL。我读过,我可以使用NSNumber的 numberWithBool boolValue 方法,但这似乎是一个很多很多额外的代码对于这么简单的东西。

I've always worked with booleans as simple BOOLs. I've read that I could use NSNumber's numberWithBool and boolValue methods, but this seems like an aweful lot of extra code for something so simple.

标题中的@property可以更改为简单的BOOL?

Can the @property in the header be changed to a simple BOOL? If so is there anything to watch out for?

感谢
-John

Thanks -John

推荐答案

简单回复:不,您不能更改 @property 声明以返回 BOOL

Simple response: No, you cannot change the @property declaration to return a BOOL.

你可以写一些简单的包装器。我将该属性重命名为 currentlyForSale (这意味着它生成 currentlyForSale setCurrentlyForSale: / code>),然后写两个包装:

You can write some simple wrappers, though. I'd rename the attribute to currentlyForSale (which means it generates currentlyForSale and setCurrentlyForSale:), and then write two wrappers:

- (BOOL) isCurrentlyForSale {
  return [[self currentlyForSale] boolValue];
}

- (void) setIsCurrentlyForSale:(BOOL)forSale {
  [self setCurrentlyForSale:[NSNumber numberWithBool:forSale]];
}

这篇关于核心数据和BOOL设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆