I’m really at a loss for words:
DDLogVerbose(@"%@", ([SO2Settings settings].drawMode & SO2DrawModeEraser) ? @"YES" : @"NO");
kMenu.eraserButton.selected = ([SO2Settings settings].drawMode & SO2DrawModeEraser);
DDLogVerbose(@"%@", kMenu.eraserButton);
DDLogVerbose(@"%@", kMenu.eraserButton.selected ? @"YES" : @"NO");
produces this output
Verbose 2012-08-05 16:21:05.391 | YES
Verbose 2012-08-05 16:21:05.391 | < UIButton: 0x6cfa380; frame = (187 10; 59 59); opaque = NO; tag = 3; layer = < CALayer: 0x6c37cc0>>
Verbose 2012-08-05 16:21:05.391 | NO
Clearly, the value of selected should be YES, yet it is NO…what is going on here?
WEIRD UPDATE : The below code produces a very unnerving result:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.selected = 8;
btn.enabled = 8;
NSLog(@"Button is selected : %@, Button is enabled : %@", btn.selected ? @"YES" : @"NO", btn.enabled ? @"YES" : @"NO");
Button is selected : NO, Button is enabled : YES
LAST UPDATE : Forget about the C-standard thing since BOOL is actually a signed char. However, I think I know the reason why enabled works but selected doesn’t. In the header, it appears that UIControl uses a bitfield that has such areas as selected (which is a 1-bit field). If it is an odd number it works correctly, but an even number doesn’t. So the number must be 0 or 1 when inserting. enabled is an actual BOOL property and is probably stored as a signed char instead of a 1-bit field.
This is not assigning a boolean YES or NO to the button:
You are assigning the result of a bitwise operation to the button. So, ([SO2Settings settings].drawMode & SO2DrawModeEraser) is either a zero or non-zero value. For zero value, it will work as boolean NO is also 0. But for non-zero value that is not equal to 1, it is not the same as boolean YES (which is equal to 1). Testing will give a boolean result, but assigning value is different.
For example:
is not the same as
You should use the following line which check for non-zero result (not boolean) and return a boolean YES to the button.
Or do u mean to use this:
Note the “&&” for logical AND instead of bitwise AND.