Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [14]

By Root 907 0
object reads the main NIB file and unarchives the objects inside. The objects are all sent the message awakeFromNib. Then the application object checks for events. The timeline for these events appears in Figure 2.25.

Figure 2.25. A Timeline

When it receives an event from the keyboard mouse, the window server puts the event data into the event queue for the appropriate application, as shown in Figure 2.26. The application object reads the event data from its queue and forwards it to a user interface object, such as a button, and your code gets triggered. If your code changes the data in a view, the view is redisplayed. Then the application object checks its event queue for another event. This process of checking for events and reacting to them constitutes the main event loop.

Figure 2.26. The Role of the Window Server

When the user chooses Quit from the menu, NSApp is sent the terminate: message. This ends the process, and all your objects are destroyed.

Puzzled? Excited? Move on to the next chapter so we can fill in some blanks.

Chapter 3. Objective-C


Once upon a time, a man named Brad Cox decided that it was time for the world to move toward a more modular programming style. C was a popular and powerful language. Smalltalk was an elegant untyped object-oriented language. Starting with C, Brad Cox added Smalltalk-like classes and message-sending mechanisms. He called the result Objective-C. Objective-C is a very simple extension of the C language. In fact, it was originally just a C preprocessor and a library.

Objective-C is not a proprietary language. Rather, it is an open standard that has been included in the Free Software Foundation’s GNU C compiler (gcc) for many years. More recently, Apple has become heavily involved in the clang/LLVM (Low Level Virtual Machine) open source compiler projects, which are much faster and more versatile than gcc. In Xcode projects, LLVM is the default compiler.

Cocoa was developed using Objective-C, and most Cocoa programming is done in Objective-C. Teaching C and basic object-oriented concepts could consume an entire book. This chapter assumes that you already know a little C and something about objects and introduces you to the basics of Objective-C. If you fit the profile, you will find learning Objective-C to be easy. If you do not, our own Objective-C Programming: The Big Nerd Ranch Guide or Apple’s The Objective-C Language offer more gentle introductions.

Creating and Using Instances


Chapter 1 mentioned that classes are used to create objects, that the objects have methods, and that you can send messages to the objects to trigger these methods. In this section, you will learn how to create an object and send messages to it.

As an example, we will use the class NSMutableArray. You can create a new instance of NSMutableArray by sending the message alloc to the NSMutableArray class like this:

[NSMutableArray alloc];

This method returns a pointer to the space that was allocated for the object. You could hold onto that pointer in a variable like this:

NSMutableArray *foo;

foo = [NSMutableArray alloc];

While working with Objective-C, it is important to remember that foo is just a pointer. In this case, it points to an object.

Before using the object that foo points to, you would need to make sure that it is fully initialized. The init method will handle this task, so you might write code like this:

NSMutableArray *foo;

foo = [NSMutableArray alloc];

[foo init];

Take a long look at the last line; it sends the message init to the object that foo points to. We would say, “foo is the receiver of the message init.” Note that a message send consists of a receiver (the object foo points to) and a message (init) wrapped in brackets. You can also send messages to classes, as demonstrated by sending the message alloc to the class NSMutableArray.

The method init returns the newly initialized object. As a consequence, you will always nest the message sends like this:

NSMutableArray *foo;

foo = [[NSMutableArray alloc]

Return Main Page Previous Page Next Page

®Online Book Reader