Objective-C Tutorial: Sending Messages

Pastedgraphic
In every object-oriented language, we have ways of calling methods within a class, and with Objective-C, we refer to this action as sending messages. Invoking such methods aids in allowing you to understand how to work with Objective-C technologies such as Key Value Observing (KVO), notifications and so forth. 

‘Learn Objective-C for Java Developers‘ defines the following steps in sending and dispatching a message to an object:

  1. Parameters needed to be sent are placed onto the stack.
  2. Pointer to the object receiving and message-selector constant also placed onto the stack.
  3. Message Dispatcher called and ifr the receiver value is nil, in which case the function returns immediately,  
  4. Selector looks up address of method’s code and CPU program counter is loaded with the address and the method’s code begins execution.
  5. Execution goes on until the method terminates and control gets returned back to the caller/sender.

Note :: class method receives message parameters, in addition to the receiver and the selector. The self variable 

Other Examples of messages
UI Controls, like buttons and UITables contain one or more actions. This consists of a receiver (which is a target) and a selector (as an action property), and when the control is interacted with, the action gets sent as a message to the target.

In Notifications, such as with NSTimer, notifications are sent through a service (similar to a postal service) to an object based on an event occurring (listened to), based on a criteria for enabling such an event. Delegates are another example of message orchestration, using selectors. 

There are three general ways of composing messages, with the most notable one being the –performSelector: methods, as done by the NSObject root, which allows for any object to send a method to any other object:


..
SEL variableMesage = @selector(className);
name = [anything performSelector:variableMessage];
..

While this method is lightweight and quick, it can only accept none, one or two object identifiers as parameters and restricts the return as an object identifier (id). So if you need to have more than two parameters, you will need to use another technique. 

Picture_1
Note: Taken from textbook Learn Objective-C for Java Developer

Deferring Messages
Queuing a message for use in the future is referred as deferring, based on a timer event or some sort of elapsed invocation, perhaps in a different thread (i.e button pressed etc, which we see in the UI with buttons):

Picture_2
Note: Taken from textbook Learn Objective-C for Java Developer

For more on this topic and other topics, check out the following book:

Learn Objective-C for Java Developers
Advertisements
This entry was posted in General and tagged . Bookmark the permalink.

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