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.
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.
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.
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 an object does not respsond to a message, it can foreward this message or use dynamic method resolution.
Based on messages a couple of Objective-C patterns can be implemented.