Cocoa Programming for Mac OS X - Aaron Hillegass [6]
With Mac OS 10.5, Objective-C underwent a major revision. All the code in this book is Objective-C 2.0, and almost all of the code in this book uses ARC for memory management. We will discuss memory management in further detail in Chapter 4.
The Objective-C code will be compiled by the LLVM compiler. The compiler allows you to freely mix C, C++, and Objective-C code in a single file.
The debugger, gdb or lldb, will be used to set breakpoints and browse variables at runtime. Objective-C gives you a lot of freedom to do dumb things; you will be glad to have a decent debugger.
Objects, Classes, Methods, and Messages
All Cocoa programming is done using object-oriented concepts. This section very briefly reviews terms used in object-oriented programming. If you have not done any object-oriented programming before, we recommend that you read The Objective-C Language. The PDF file for the book is on the Apple Web site, The URL is http://developer.apple.com/library/mac/documentaion/Cocoa/Conceptual/ObjectiveC/ObjC.pdf.
What is an object? An object is like a C struct: It takes up memory and has variables inside it. The variables in an object are called instance variables. So when dealing with objects, the first questions we typically ask are: How do you allocate space for one? What instance variables does the object have? How do you destroy the object when you are done with it?
Some of the instance variables of an object will be pointers to other objects. These pointers enable one object to “know about” another.
Classes are structures that can create objects. Classes specify the variables that the object has and are responsible for allocating memory for the object. We say that the object is an instance of the class that created it (Figure 1.2).
Figure 1.2. Classes Create Instances
An object is better than a struct because an object can have functions associated with it. We call the functions methods. To call a method, you send the object a message (Figure 1.3).
Figure 1.3. Messages Trigger Methods
Frameworks
A framework is a collection of classes that are intended to be used together. That is, the classes are compiled together into a reusable library of code. Any related resources are put into a directory with the library. The directory is renamed with the extension .framework. You can find the built-in frameworks for your machine in /System/Library/Frameworks. Cocoa is made up of three frameworks:
1. Foundation: Every object-oriented programming language needs the standard value, collection, and utility classes. Strings, dates, lists, threads, and timers are in the Foundation framework.
2. AppKit: All things related to the user interface are in the AppKit framework. These include windows, buttons, text fields, events, and drawing classes. You will also see this framework called the ApplicationKit.
3. Core Data: Core Data makes it easy to save your objects to a file and then reload them into memory. We say that it is a persistence framework.
We will focus on these three frameworks because they are the most commonly used. Once you have mastered these, the other frameworks will be easier to understand. Numerous other frameworks handle such duties as encryption, QuickTime, and CD burning.
You can also create your own frameworks from the classes that you create. Typically, if a set of classes is used in several applications, you will want to turn them into a framework.
How to Read This Book
This book acts as the guide through activities to help you understand Cocoa programming. Often, we will ask you to do something and explain the details or theory afterward. If you are confused, read a little more. Usually, the help you seek will be only a paragraph or two away.
If you are still stumped, you can get help on the Web site for this book: www.bignerdranch.com/books. Errata, hints, and examples are listed there as well. Also, all