I have a BOOL property that I want to set in my class initializer.
@property (assign, nonatomic) BOOL isEditMode;
- (id)init
{
. . .
[self setValue:NO forKey:isEditMode];
return self;
}
The compiler gives me an “Incompatible integer to pointer conversion” warning. What am i doing wrong here?
The Key-Value Coding method
setValue:forKey:only accepts objects as arguments. To set a BOOL, you need to wrap the number in a value object with[NSNumber numberWithBool:NO]. But there’s little reason to do that. Key-Value Coding is a roundabout way to accomplish this. Either doself.isEditMode = NOor justisEditMode = NO. The latter is preferable in an init method (because setters can run arbitrary code that might not be desirable before an object is fully set up).But to elaborate on the first point: The reason Key-Value Coding works this way is because the type system can’t represent an argument that’s sometimes an object and at other times a primitive value. So KVC always deals with objects and just autoboxes primitive values as necessary. Similarly, if you do
[yourObject valueForKey:@"isEditMode"], you’ll get back an NSNumber object wrapping the real value.