Search code examples
objective-cif-statementbooleannslog

If-else not executing block when bool is true?


This could just as easily be something stupid I missed, but take a look at this code: (which has been stripped down to just debug functionality, although the actual method name was left unchanged)

-(BOOL)shouldHideStatusBarItem:(BOOL)showItem{

if (showItem == YES) {
    NSBeep();
    NSLog(showItem ? @"YES(inloop)" : @"NO(inloop)" );
}
else if (showItem == NO){
    NSBeep();
    NSLog(showItem ? @"YES(inloop)" : @"NO(inloop)" );
}

NSLog(showItem ? @"YES" : @"NO" );
return showItem;
}

When I pass in YES it logs:

YES

When I pass in NO it logs:

 NO(inloop)
 NO

Obviously when I pass in YES it SHOULD log:

YES(inloop)
YES

Does anybody have any ideas?


Solution

  • Change your code to just check the truth of the showItem variable. The BOOL type is not actually restricted to the values YES and NO.

    if (showItem)
    {
        ...
    }
    else
    {
        ...
    }