I read Mike Ash's Objective-C pitfalls page, and now I'm paranoid about implicitly casting variables of type id
to BOOL
.
Assume I have a 'dangerous' pointer with the lower bits zeroed out, so that casting it to a BOOL
would produce NO
even though it points to a valid object. Let's call this 'dangerous' pointer foo
.
How do simple if statements work?
Does the if statement cast foo
to a BOOL
when it evaluates the condition? Or does it cast to a pure boolean?
if( foo )
{
[myArray addObject:foo];
}
Does it work the same way in ternary expressions?
// Does this break when foo is 'dangerous'?
self.titleLabel.hidden = foo ? YES : NO ;
Or will the ternary only break if I do this:
// I'm pretty sure it breaks now
self.titleLabel.hidden = ((BOOL)foo) ? YES : NO ;
I feel like I'm missing something basic about logical operations in C, and how they relate to BOOL
. Please help enlighten me.