Let’s say I have a pointer to some object, called myObject, and I need to know, whether it is really pointing to something. How can this code:
// assume MyObjectClass *myObject;
return (BOOL)myObject;
return 112? I know, that I can always write
return (myObject == nil);
and everything will be fine. But until today I have always assumed, that explicit casting of anything to bool will always return true or false (as far as I know, 0 is always considered as false and any other value as true) and that BOOL with it’s YES and NO values is just “renamed” bool. So basically, my questions are:
- Why is it returning 112? 🙂
- Are results of explicit casting defined somewhere in C/Objective-C standard, or is it compiler-specific?
In Objective-C, the
BOOLmacro is just atypedefforsigned charwithYES/NOdefined, butboolis an actual boolean, which can betrueorfalse.It returns 112, because it rounds the address of your pointer to
signed chartype.Here is some discussion with good answers:
Objective-C : BOOL vs bool
Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?