开发者

Synthesize a BOOL to set value in Objective C

I have created a BOOL attribute in my Core Data entity called "useSystem". Additionally in order for me to get/set the data I have created an object, however whenever I try to set the synthesized BOOL I get a bus error. This is my code:

@property (nonatomic) const BOOL useSystem;

开发者_开发技巧So I'm doing

[object setUseSystem:YES];

And immediately I get the bus error. Can anyone help?


Use [NSNumber numberWithBool:YES]


It is probably better style to use NSNumber as the property type actually. This is also what happens when you use the model editor in xcode to add a boolean attribute to an entity. There is 'auto-boxing' going on but I always seem to have less troubles when I just use the higher level Objective-C types and wrappers like NSNumber.


This should work if you declare the method explicitly but it might not work with @synthesize. Seems like I've done this before, but maybe I just used the BOOL with no mutator method. (You can use @property without @synthesize if you define the method(s) that @property declares.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜