Zillan
Zillan

Reputation: 720

Objective-C: Returning int to BOOL method

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

Thank you

-(BOOL) isEnabled{
   return 56;
}

Upvotes: 5

Views: 6719

Answers (2)

James Webster
James Webster

Reputation: 32066

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

Upvotes: 0

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

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.

Upvotes: 6

Related Questions