0

I've looking information related with IMP, but everything I find looks like an old practice.

I may be wrong but IMP represents the address of a method so you can pass it as an argument if you need to, right? If this is true, doesn't @selector allow this?

I would appreciate if somebody can explain this to me because I'm really lost. It would be nice I can be provided with some information about using IMP and @selector.

Diego A. Rincon
  • 747
  • 1
  • 8
  • 25

1 Answers1

1

@selector resolves to a SEL which can be used to look up an IMP. As you surmised, an IMP points to the implementation of a function, otherwise known as a function pointer.

What looks like this [NSObject alloc] in objective C looks like this in C:

objc_msgSend(objc_getClass("NSObject"),sel_getUid("alloc"))

Internally, objc_msgSend will take the provided Class and SEL and use something like class_getMethodImplementation to get the IMP to invoke.

It is generally better to use higher level interfaces like Method and functions like NSSelectorFromString and NSClassFromString to deal with the runtime.

drawnonward
  • 53,459
  • 16
  • 107
  • 112