Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Objective-C methods are invoked by messaging, rather than by direct function calls. A message expression consists of a receiver (the object whose method is being called) and a message (the name of the method to be executed, plus any arguments that the method takes) enclosed in square brackets. When a message expression is evaluated, the runtime determines the receiver’s class and then looks in the class’s class structure to find a pointer to the code that implements the method. The runtime then fills in the hidden variables self (a pointer to the receiver) and _cmd (the name of the method, converted to a SEL) and uses the pointer to execute the code.
This arrangement has several advantages. Both parts of the message expression can be dynamic; they can be determined at runtime rather than compile time. It also allows for complete polymorphism. If the receiver is typed as id (pointer to object), it can hold any object as long as the object’s class implements a method with a name that corresponds to the name in the message. Polymorphism is used when you need to elicit the same type of behavior from a heterogeneous collection of objects—for example, asking each object in a heterogeneous collection to draw itself.