7stud
7stud

Reputation: 48659

Why can I pass a BOOL to a method that requires an NSInteger argument?

Why can I declare/define a method like this:

@interface Dog : NSObject

- (void) doStuff:(NSInteger)val;

@end

...

@implementation Dog

- (void) doStuff:(NSInteger)val {

    NSLog(@"arg was valid");

}

@end

...and call it like this:

Dog* mydog = [[Dog alloc] init];
[mydog doStuff:YES];  //=>arg was valid

I've read that BOOL is a typedef for a signed char. Usually in Xcode6, if the types don't exactly match, I get all kinds of warnings that tell me to cast to the proper type, and Xcode will insert the casts for me if I click on the right spot.

Upvotes: 0

Views: 51

Answers (1)

CRD
CRD

Reputation: 53010

In the C family of languages - (Objective-)C(++) - the various boolean types (BOOL, bool, _Bool) are all classed as integer types. (char is also an integer type.)

Using a smaller integer type where a larger integer type is required is an implicit conversion, no cast is required.

Combine those and you can pass a BOOL as an NSInteger.

HTH

Upvotes: 1

Related Questions