The message (iOS 4)

Objective-C’s most important extension to the C programming language is the message. A message is sent when one object asks another to perform a specific action; it’s Objective-C’s equivalent to the procedural functional call. Messages are also where Objective-C’s syntax varies the most from ANSI C standards—which means that when you understand them, you’ll be able to read most Objective-C code. A simple message call looks like this:


Here’s a real-life example that you’ll meet in the next topic:


That message sends the window object the makeKeyAndVisible: command, which tells it to appear and start accepting user input.

There are three ways in which this message could be slightly more complex. First, it could accept arguments; second, it could be nested; and third, it could be a call to one of a few different recipients.

Messages with arguments

Many messages include a simple command, as in the previous example. But sometimes you’ll want to send one or more arguments along with a message to provide more information about what you want done. When you send a single argument, you do so by adding a colon and the argument after the message, like so:


Here’s another real-world example:


When you want to send multiple arguments, each additional argument is sent following a label, as shown here:


This is the way in which Objective-C’s messages vary the most from C’s functions. You’re going to come to love it. You no longer need to remember the ordering of the arguments because each gets its own title, clearly marking it. The result is much more readable.

Nested messages

One of the most powerful elements of Objective-C’s messaging system is the fact that you can nest messages. This allows you to replace either the recipient or the argument of a message (or both) with another message. Then, the return of that nested message automatically fills in the appropriate space of the message it’s nested inside. Object creation frequently replaces the receiver in this manner:


The object created by sending the alloc message to the UITextView class object is then initialized. (We’ll get to class objects in a moment.)

When you’re passing a color as an argument, you almost always do so by nesting a call to the UIColor class object:


Message nesting is a core Objective-C coding style, and you’ll see it frequently. It also shows why Objective-C’s bracketed messaging style is cool. With good use of code indentation, it can make complex concepts readable.

Message recipients

As you’ve seen over the last couple of examples, Objective-C uses two different types of objects. Class objects innately exist, and each represents one of the classes in your framework. They can be sent certain types of requests, such as a request to create a new object, by sending a message to the class name:


Here’s an example:


Instance objects are what you’re more likely to think of when you hear the term object. You create them yourself, and the majority of your programming time is spent manipulating them. Except for those examples of creating new objects, all of our real-life examples so far have involved instance objects.

In addition to calling an object by name, you can also refer to an object by one of two special keywords: self and super. The first always refers to the object itself, whereas the second always refers to the class’s parent.

You’ll often see self used internal to a class’s source code file:


You’ll often see super used as part of an overridden method, where the child calls the parent’s method before it executes its own behavior:


All your message calls should follow one of these four patterns when naming its receiver: they can call something by its class name (for a class method), by its instance name (for an instance method), by the self keyword, or by the super keyword.

Now that you know how to send messages between objects, you’d probably like to know how to create those classes from which your objects are instantiated in the first place. That’s the topic of the next section.

Next post:

Previous post: