Search code examples
objective-cswizzlingmethod-swizzling

Objective C - What is the difference between IMP and function pointer?


I recently started a project where I require to do swizzling.

After going through many tutorials I got a question, What is the difference between Implementation and function pointer?


Solution

  • From memory, an IMP is a memory-address just like a function pointer, and can be invoked just like an ordinary C function. However it is guaranteed to use objective-C messaging convention, where:

    • The first argument is the object to operate on (self).
    • The second argument is the _cmd (SELECTOR) to be invoked. I believe this is so to support dynamic features, such as ObjC message forwarding where we could wrap the original implementation in a proxy, say to start a transaction or perform a security check, or, for a Cocoa specific example, add some property observation cruft, by magic, at run-time. While we already have the function signature, I could be helpful, in some cases, to know "how did I get here?" with the message signature.
    • Following arguments, if any, are according to the method contract.