Passing in a custom selector implementation-Collection of common programming errors

You can certainly use the runtime functions to do something like this,* but I’d suggest that this is exactly the sort of problem that Blocks were introduced to solve. They allow you to pass around a chunk of executable code — your method can actually accept a Block as an argument and run it.

Here’s a SSCCE:

#import 

typedef dispatch_block_t GenericBlock;

@interface Albatross : NSObject 
- (void)slapFace:(NSNumber *)n usingFish:(GenericBlock)block;
@end

@implementation Albatross

- (void)slapFace:(NSNumber *)n usingFish:(GenericBlock)block
{
    if( [n intValue] > 2 ){
        NSLog(@"Cabbage crates coming over the briny!");
    }
    else {
        block();    // Execute the block
    }
}

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Albatross * p = [Albatross new];
        [p slapFace:[NSNumber numberWithInt:3] usingFish:^{
            NSLog(@"We'd like to see the dog kennels, please.");
        }];
         [p slapFace:[NSNumber numberWithInt:1] usingFish:^{
            NSLog(@"Lemon curry?");
        }];

    }
    return 0;
}

*Note that using method_setImplementation() will change the code that’s used every time that method is called in the future from anywhere — it’s a persistent change.