1. Introduction

The “C” programming language was pioneered by Dennis Ritchie at AT&T Bell Laboratories in the early 1970s. It was not until the late 1970s, however, that this programming language began to gain widespread popularity and support. This was because until that time C compilers were not readily available for commercial use outside of Bell Laboratories. Initially, C's growth in popularity was also spurred on in part by the equal, if not faster, growth in popularity of the Unix operating system, which was written almost entirely in C.

The Objective-C language was designed by Brad J. Cox in the early 1980s. The language was based on a language called SmallTalk-80. Objective-C was layered on top of the C language, meaning extensions were added to C to create a new programming language that enabled objects to be created and manipulated.

NeXT Software licensed the Objective-C language in 1988 and developed its libraries and a development environment called NEXTSTEP. In 1992, Objective-C support was added to the Free Software Foundation's GNU development environment. This software is in the public domain, which means anyone who wants to learn how to program in Objective-C can do so by downloading its tools at no charge.

In 1994 NeXT Computer and Sun Microsystems released a standardized specification of the NEXTSTEP system called OPENSTEP. The Free Software Foundation's implementation of OPENSTEP is called GNUStep. There's also a Linux version that includes the Linux kernel and the GNUStep development environment called, appropriately enough, LinuxSTEP.

On December 20, 1996, Apple Computer announced that it was acquiring NeXT Software and the NEXTSTEP/OPENSTEP environment became the basis for the next major release of Apple's operating system, OS X. Apple's version of this development environment was called Cocoa. With built-in support for the Objective-C language, coupled with development tools such as Project Builder (or its successor Xcode) and Interface Builder, a powerful development environment was created for application development on Mac OS X.

When contemplating writing a tutorial on Objective-C, a fundamental decision had to be made. As with all prior texts on Objective-C, I could write mine to assume the reader already knew how to write C programs. I could also teach the language from the perspective of using the rich library of routines, such as the Foundation and Application Kit frameworks. Some texts also take the approach of teaching how to use the development tools, like the Mac's Project Builder and Interface Builder.

I had several problems adopting this approach. First, learning the entire C language before learning Objective-C is wrong. C is a procedural language containing many features that are not necessary for programming in Objective-C, especially at the novice level. In fact, resorting to some of these features goes against the grain of adhering to a good object-oriented programming methodology. It's also not a good idea to teach all the details of a procedural language before learning an object-oriented one. This starts the programmer off in the wrong direction, and gives the wrong orientation and mindset for fostering a good object-oriented programming style. Just because Objective-C is an extension to the C language doesn't mean you have to learn C first!

So, I decided neither to teach C first nor to assume prior knowledge of the language. Instead, I decided to take the unconventional approach of teaching Objective-C and the underlying C language as a single integrated language, and from an object-oriented programming perspective. The purpose of this book is as its name implies: to teach you how to program in Objective-C. It makes few assumption about the platform you're running under, the development tools you have available, or the library of classes at your disposal. All that material can be learned elsewhere, after you've learned how to write programs in Objective-C. In fact, mastering that material will be much easier after you have a solid foundation on how to program in Objective-C. This book does not assume much, if any, previous programming experience. In fact, if you're a novice programmer, you should be able to learn Objective-C as your first programming language from this text.

This book teaches Objective-C by example. As each new feature of the language is presented, a small complete program example is usually provided to illustrate the feature. Just as a picture is worth a thousand words, so is a properly chosen program example. If you have access to an Objective-C compiler, you are strongly encouraged to run each program (all of which are available online) and compare the results obtained on your system to those shown in the text. By doing so, not only will you learn the language and its syntax, but you will also become familiar with the process of compiling and running Objective-C programs.

This book is divided into two logical parts. Part I, “The Objective-C Language,” teaches the essentials of the language. Part II, “The Foundation Framework,” teaches how to use the rich assortment of predefined classes that form the Foundation framework.

A framework is a set of classes and routines that have been logically grouped together to make developing programs easier. Much of the power of programming in Objective-C rests on the extensive frameworks that are available. The Foundation framework has been implemented across many different systems, including Unix (and its variants such as Linux and Solaris), Windows (under a Unix layer such as CygWin or MinGW), and Mac OS X. By using the classes in the Foundation framework, you can take advantage of a powerful assortment of built-in classes and develop your programs more quickly and in a more portable manner.

Chapter 2, “Programming in Objective-C,” begins by teaching you how to write your first program in Objective-C. Special attention is given to how to enter, compile, and run Objective-C programs on Mac OS X and on Windows and non-Mac Unix systems using the GNU gcc compiler.

Because developing graphical user interfaces (GUIs) is not taught in this book, a method was needed to get input and produce output. Most of the examples in this text are command-line driven, so they take input from the keyboard and produce their output in a window, perhaps a Terminal window under Unix or Mac OS X, or an MS-DOS window under Windows.

Chapter 3, “Classes, Objects, and Methods,” covers the fundamentals of object-oriented programming. Some terminology is introduced here, but it's kept to a minimum. The mechanism for defining a class is introduced in the chapter, and the means for sending messages to instances or objects are also covered. Instructors and seasoned Objective-C programmers will notice that I use static typing for declaring objects. I think this is the best way for the student to get started, from the perspective of allowing the compiler to catch more errors, making the programs more self-documenting, and encouraging the new programmer to explicitly declare her data types when they are known. As a result, the notion of the id type and its power is not fully explored until Chapter 9, “Polymorphism, Dynamic Typing, and Dynamic Binding.”

Chapter 4, “Data Types and Expressions,” describes the basic Objective-C data types and how to use them in your programs.

Chapter 5, “Program Looping,” introduces the three looping statements you can use in your programs: for, while, and do.

Making decisions is fundamental to any computer programming language. Chapter 6, “Making Decisions,” covers the Objective-C language's if and switch statements in detail.

Chapter 7, “More on Classes,” delves more deeply into working with classes and objects. Details about methods, multiple arguments to methods, and local variables are discussed here.

Chapter 8, “Inheritance,” introduces the key concept of inheritance. This feature makes the development of programs easier because you can take advantage of what comes from above. Inheritance and the notion of subclasses make modifying and extending existing class definitions easy.

Chapter 9 discusses three fundamental characteristics of the Objective-C language. Polymorphism, dynamic typing, and dynamic binding are the key concepts covered here.

Chapters 1013 round out the discussion of the Objective-C language, covering issues such as initialization of objects, protocols, categories, the preprocessor, and some of the underlying C features, including functions, arrays, structures, and pointers. These underlying features are often unnecessary (and often best avoided) when first developing object-oriented applications. It's recommended you skim Chapter 13, “Underlying C Features,” the first time through the text and return to it only as necessary to learn more about a particular feature of the language.

Part II begins with Chapter 14, “Introduction to the Foundation Framework,” which gives an introduction to the Foundation framework and its root object called NSObject.

Important features of the Foundation framework are covered in Chapters 1519. These include working with number and string objects, collections, working with the file system, memory management, and copying and archiving objects.

By the time you're done with Part II, you will be able to develop fairly sophisticated programs in Objective-C that work with the Foundation framework.

Because object-oriented parlance involves a fair amount of terminology, Appendix A, “Glossary,” provides definitions of some common terms.

Appendix B, “Objective-C Language Summary,” gives a summary of the Objective-C language and is provided for your quick reference.

In Appendix C, “Foundation Framework Headers,” a brief summary of the Foundation framework header files is provided. This list will give you a quick idea of the types of capabilities that are offered by this framework.

Appendix D, “Fraction and Address Book Examples,” gives the source code listing for three classes that are developed and used extensively throughout this text. One class is for working with fractions. The other two enable you to define an address book and perform various operations with it.

After you've learned how to write Objective-C programs, you can go in several directions. You might want to lean more about the underlying C programming language, or you may want to start writing Cocoa programs to run on Mac OS X. In any case, the resources listed in Appendix E, “Resources,” will guide you in the right direction.

All the program examples in this book were run on Mac OS X Version 10.2.6 as well as using GNUStep base version 1.6.0 under MinGW on a Windows XP system. The text teaches you how to write Objective-C programs that are portable and independent of any particular operating system and machine architecture.

There are several people I would like to acknowledge for their help in the preparation of this text. First, I want to thank Tony Iannino and Steven Levy for reviewing the manuscript. I am also grateful to Mike Gaines for providing his input.

I'd also like to thank my technical editors, Jack Purdum and Mike Trent. Mike provided the most thorough review of any book I've ever written. Not only did he point out some weaknesses, but he was also generous enough to offer his suggestions as well. Because of Mike's comments, I changed my approach to teaching memory management and tried to make sure that every program example in this book is “leak-free.”

From Sams Publishing I'd like to thank my editor Kathryn Mohr for, well, just for everything. It was a pleasure working with Katie on this book and I am sorry that I won't get a chance to work with her again at Sams. Also from Sams I'd like to thank Scott Meyers for his help and for taking over where Katie left off.

Catherine Babin supplied the cover photograph and provided me with many wonderful pictures from which to choose. Having the cover art from a friend makes the book even more special.

My children showed an incredible amount of maturity and patience while I pulled this book together over their summer vacation. To Gregory, Linda, and Julia, I love you!

Stephen G. Kochan
August 2003

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.240.249