Core Data实体中的布尔属性的类型是什么? [英] What is the type for boolean attributes in Core Data entities?

查看:160
本文介绍了Core Data实体中的布尔属性的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以与Apple的 .xcdatamodel 数据模型文件) developer.apple.com/documentation/Cocoa/Conceptual/CoreDataUtilityTutorial/CoreDataUtilityTutorial.pdfrel =nofollow noreferrer> Core Data Utility Tutorial 。所以我的问题是,当我尝试添加一个属性到一个实体类型 NSBooleanAttributeType ,它得到一个有点儿bug。当我把它添加到我的 NSManagedObject 子类头文件(在本教程中,将 Run.h / p>

I am using Core Data programmatically (i.e. not using .xcdatamodel data model files) in much the same manner as depicted in Apple's Core Data Utility Tutorial. So my problem is that when I try to add an attribute to an entity with the type NSBooleanAttributeType, it gets a bit buggy. When I add it to my NSManagedObject subclass header file (in the tutorial, that would be Run.h) as

@property (retain) BOOL *booleanProperty;

编译失败,说错误:property'booleanProperty'with'retain'attribute必须是对象类型

似乎Cocoa中的一些地方使用 NSNumber 对象代表布尔值,所以我试着设置它

It seems like some places in Cocoa use NSNumber objects to represent booleans, so I tried setting it to

@property (retain) NSNumber *booleanProperty;

。但是,这引起以下运行时错误:

instead. However, this evokes the following runtime errors:

*** -[NSAttributeDescription _setManagedObjectModel:]: unrecognized selector sent to instance 0x101b470
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSAttributeDescription _setManagedObjectModel:]: unrecognized selector sent to instance 0x101b470'

使用GDB,我可以追溯到我的源代码中我添加我的实体到管理对象模型的行:

Using GDB, I am able to trace this back to the line in my source code where I add my entity to the managed object model:

[DVManagedObjectModel setEntities:[NSArray arrayWithObjects:myEntityWithABooleanAttribute, myOtherEntity]];

所以我的问题是:我应该在自定义类头中设置booleanProperty类型? p>

So my question is this: what type should I set booleanProperty to in my custom class header?

推荐答案

尝试:

@property (nonatomic) BOOL booleanProperty;

问题是在属性定义中使用了retain。为此,你必须有一个Objective-C类的属性(它应该能够理解'retain'方法)。 BOOL不是类,而是signed char的别名。

The problem was that you used the retain in the property definition. For that you must have a property for an Objective-C class (it should be able to understand the 'retain' method). BOOL is not a class but an alias for signed char.

这篇关于Core Data实体中的布尔属性的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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