04 – From SEL to IMP – or how to call a method

The way how methods are called in Objective-C is different to Java. If you are doing your first steps with Objective-C, it seems that sending messages to an object in Objective-C is exactly the same as calling methods in Java. A different syntactic way.

But sending messages and calling methods are two completely different things in Objective-C. A sent message will be converted into a method call by the message retrieving object.

In Java you just call the method.

MyObject.sayHello() calls the method directly

In Objective-C you do not directly call methods for normal. Instead you are sending messages to an object and let the message receiving object converts the message to a method call.

[MyObject sayHello] - The message is converted to a method call by MyObject

The message sent to an object is converted into an selector:

  • A message is a request to perform a specific method for an object. The message receiving object determines the method which should be performed based on the sent message by a selector.
  • The selector identifies the message sent to an object. A compiled selector identifies the method name of an object. The message receiving object uses the selector to select the method which should be called. Objective-C provides its own datatype for selectors.

SEL datatype

The SEL datatype is used to declare variables which store selectors.

SEL mySelector = @selector(doSomething);

“doSomething” is the message which will be sent to an object.

The defined selector can be used to send messages to an object too.

[myObject doSomething]
[myObject performSelector:mySelector]
[myObject performSelector:@selector(doSomething)]
[myObject performSelector:NSSelectorFromString(@"doSomething")];

These ways of sending messages will be treated equal by the receiving object.

It will select the method to be invoked based on the selector and calls it. Therefore C functions are used to determine the function pointer which identifies the method implementation. For detailed informations see developer.apple.com.

This instance method pointers (a function pointer to a function which implements an Objective-C method ) are stored in IMP variables.

IMP is defined by:

typedef id (*IMP)(id self,SEL _cmd,...);

To access the IMP, the message “methodForSelector” can be used.

IMP myImpDoSomething = [myObject methodForSelector:@selector(doSomething)];

The method adressed by the IMP can be called by dereferencing the IMP.

myImpDoSomething(myObject, @selector(doSomething));

So these calls are equal:

myImpDoSomething(myObject, @selector(doSomething));
[myObject doSomething]
[myObject performSelector:mySelector]
[myObject performSelector:@selector(doSomething)]
[myObject performSelector:NSSelectorFromString(@"doSomething")];

Detailed informations see developer.apple.com.

Messaging Errors

If a message is sent to an object which can not be handled, an error results.

You can use the respondsToSelector message to ensure that the receiver can handle the message.
if([myObject respondsToSelector:@selector(doSomething)]{
….
}

Message forwarding

If an object does not respsond to a message, it can foreward this message or use dynamic method resolution.

This issue is out of the scope of this blog. For detailed informations see dynamicMethodResolution and messageForwarding at developer.apple.com.

Patterns

Based on messages a couple of Objective-C patterns can be implemented.

Additional references

Selectors

Interacting with the Runtime

at developer.apple.com

Advertisements

Tags: , , , , , , , ,

One Response to “04 – From SEL to IMP – or how to call a method”

  1. iOS FAQs | Wandering in the desert - a coder's life Says:

    […] IMP: instance method pointer – a function pointer – points to an Obj-C method.IMP is defined by: […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: