For Objective-C ... Pointer to method

dispatch, methods, objective-c, pointers

Solution

Objective-C methods are called `selector`s, and are represented by the `SEL` datatype. If your object inherits from `NSObject`, you can tell it to perform a selector (i.e. call a method) like thus:

SEL selector = @selector(doSomething:);
[obj performSelector:selector withObject:argument];

This assumes you have a method defined such as:

-(void)doSomething:(MyObject*)arg;

Selectors are assigned to `SEL` datatypes through the `@selector` keyword, which takes the name of the method you would like to keep. The name of the method is the method name stripped of all arguments. For example:

-(void)doSomething:(MyObject*)arg withParams:(MyParams*)params

Would be referenced as `@selector(doSomething:withParams:)`.

Problem

I want to setup a Method dispatch table and I am wondering if it is possible to create pointer to a method in Objective-C (like pointer to function in C). I tried to use some Objective-C runtime functions to dynamically switch methods but the problem is it will affect all instances. As I am very new to Objective-C, an illustrated example would be highly appreciated.

Original source