Leandros
Leandros

Reputation: 16825

Why is BOOL turned to int when negated with explicit block return type

Why is a BOOL, which is a typedef signed char, converted to an int when negated?

// Doesn't compile.
NSInteger occurrences = [[contactCountries indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return ![obj isEqualIgnoringCase:@"astring"];
}] count];

Error:

return type 'int' must match previous return type 'BOOL' (aka 'signed char') when block literal has unspecified explicit return type

Upvotes: 1

Views: 138

Answers (1)

DarkDust
DarkDust

Reputation: 92336

That's due to the "C" in Objective-C. C99 says this about the ! operator (emphasize mine):

6.5.3.3 Unary arithmetic operators, paragraph 5:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

Simply cast it:

return (BOOL)![obj isEqualIgnoringCase:@"astring"];

Upvotes: 1

Related Questions