Objects, Messages, and Targets

The Cocoa Application Kit is written in Objective-C, a hybrid language based on ANSI C and SmallTalk that was developed by Brad Cox. The principal building block of object-oriented programs is the object . Objects can be thought of as bundles of related ANSI C variables and functions with lots of extra functionality.

Objective-C objects are self-contained code entities that communicate by sending messages to one another. A message is like a function call in a traditional programming language in that it causes a procedure to be executed. However, a message is unlike a function call in that it is sent to a specific object; it is not simply “called.” The procedures that are executed — called methods in Objective-C — are “encapsulated” inside objects, and thus different objects can respond to the same message in different ways.

Unlike some object-oriented languages, Objective-C doesn’t require that you know the type (class) of an object before you send it a message. This is known as dynamic or runtime binding; the message is bound to the object at runtime instead of compile-time. It allows for much greater flexibility, because certain decisions can be made following a user’s action. For example, when a user chooses Cut from an application’s Edit submenu, a cut: message is sent to a target object (the colon is part of the message). If an application lets a user cut text, graphics, and other types of data, the cut: message will have varying targets ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.