5

I am new to Objective-C and i wonder why this method compiles, can anyone explain me why?

Thank you

-(BOOL) isEnabled{
   return 56;
}
Zillan
  • 720
  • 7
  • 15

2 Answers2

6

A BOOL in Objective-C is a typedef of signed char. Since 56 fits in that type, the implicit conversion from a literal int results in no data loss.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • 3
    Zillan, also note that that this may break code if it relies on a `YES` or `NO` value. E.g. `if([self isEnabled] == YES]` would fail while `if([self isEnabled])` would work. – Joe Jun 06 '12 at 13:01
  • Yep, which is why normal coding style avoids comparing against those constants, and only uses them for assignment. – Jonathan Grynspan Jun 06 '12 at 13:04
  • 3
    You should really check out this article: http://weblog.bignerdranch.com/564-bools-sharp-corners/ – Brian Palma Jun 06 '12 at 13:22
0

You can think of a BOOL in objective-c as

false === 0 === nil   //Anything that is zero or nil is false
true = !false         //Anything that is NOT zero or nil is true. 

56 therefore returns true because it is not zero

James Webster
  • 31,873
  • 11
  • 70
  • 114