Passing block parameter that doesn't match signature
objective-c, objective-c-blocks
Solution
Providing an empty arguments specification as in
typedef void(^MyBlock)();
means "unspecified" arguments. So the two types are compatible as written. Changing the first declaration to
typedef void(^MyBlock)(void);
specifies that the block takes no arguments and you'll get an error.
K&R C specifies that an empty argument list means "unspecified". The C blocks spec says this is not true for block type declarations (cf. http://clang.llvm.org/docs/BlockLanguageSpec.html#block-variable-declarations) but: both GCC and Clang implement the K&R behavior as a language extension.
Problem
I'm working with a block-based API and stumbled across a scenario where I was passing in a block parameter that had a signature that didn't match the typedef'd parameter the method was expecting. To my surprise, the compiler didn't seem to care about this, and the app didn't crash. Is this expected behavior? Example: ``` typedef void(^MyBlock)(); typedef void(^MyBlockWithParam)(id param); - (void)doWork { MyBlockWithParam block1 = ^(id param) { NSLog(@"block1: %@", param); }; MyBlock block2 = ^{ NSLog(@"block2"); }; [self loadData:block1]; [self loadData:block2]; } - (void)loadData:(MyBlockWithParam)block { block(@"foo"); } ```