我以编程方式使用 Core Data(即不使用 .xcdatamodel
数据模型文件),其方式与 Apple 的 Core Data Utility Tutorial 中描述的方式大致相同。 .所以我的问题是,当我尝试向类型为 NSBooleanAttributeType
的实体添加属性时,它会出现一些问题。当我将它添加到我的 NSManagedObject
子类头文件(在教程中,这将是 Run.h
)作为
@property (retain) BOOL *booleanProperty;
编译失败,提示error: property 'booleanProperty' with 'retain' attribute must be of object type
。
Cocoa 中好像有些地方使用NSNumber
对象来表示 bool 值,所以我尝试将其设置为
@property (retain) NSNumber *booleanProperty;
相反。但是,这会引发以下运行时错误:
*** -[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,我能够将其追溯到源代码中将我的实体添加到托管对象模型的行:
[DVManagedObjectModel setEntities:[NSArray arrayWithObjects:myEntityWithABooleanAttribute, myOtherEntity]];
所以我的问题是:我应该在我的自定义类 header 中将 booleanProperty 设置为什么类型?
最佳答案
尝试:
@property (nonatomic) BOOL booleanProperty;
问题是您在属性定义中使用了保留。为此,您必须拥有 Objective-C 类的属性(它应该能够理解“保留”方法)。 BOOL 不是类,而是 signed char 的别名。
关于objective-c - 核心数据实体中 bool 属性的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1022284/