title: Objective-C type-check a block?
body: <p>This is different from other "can I check the type of a block" posts on SO, as far as I can tell anyway.</p>
  
  <p>I want to know if, given a block object of unknown signature, I can learn what arguments it accepts prior to invoking?</p>
  
  <p>I have a situation where I have a number of callbacks associated with objects in a dictionary.  I want some of those callbacks to expect a different set of arguments.  The example here is extremely simplified, but I think it gets the point across.</p>
  
  <p>How can I find out if a block is of a type I previously typedef'd?</p>
  
  <pre><code>//MyClass.m
  
  // I start by declare two block types
  typedef void (^callbackWithOneParam)(NSString*);
  typedef void (^callbackWithTwoParams)(NSString*, NSObject*);
  
  ........
  
  // I create a dictionary mapping objects to callback blocks
  
  self.dict = @{
    @"name": "Foo",
    @"callback": ^(NSString *aString) {
      // do stuff with string
    }
  }, {
    @"name": "Bar",
    @"callback": ^(NSString *aString, NSObject *anObject) {
      // do stuff with string AND object
    }
  } 
  
  .....
  
  // Later, this method is called.  
  // It looks up the "name" parameter in our dictionary, 
  // and invokes the associated callback accordingly.
  
  -(void) invokeCallbackForName:(NSString*)name {
     // What is the type of the result of this expression?
     [self.dict objectForKey: name]
  
     // I want to say: (pseudocode)
     thecallback = [self.dict objectForKey: name];
     if (thecallback is of type "callbackWithOneParam") {
        thecallback(@"some param")
     }
     else if (thecallback is of type "callbackWithTwoParams") {
        thecallback(@"some param", [[NSObject alloc] init]);
     }
  }
  </code></pre>