What Is Objective-C?
Objective-C is an object-oriented language: it supports hierarchies of substitutable types, message-passing between objects, and code reuse through inheritance. Objective-C adds these features to the familiar C programming language.
Because Objective-C is an extension of C, many properties of an Objective-C program depend on the underlying C development tools. Among these properties are:
· The size of scalar variables such as integers and floating-point numbers
· Allowed placement of scoped declarations
· Implicit type conversion and promotion
· Storage of string literals
· Preprocessor macro expansion
· Compiler warning levels
· Code optimization
· Include and link search paths
For more information about these topics, consult the documentation for your development platform and tools.
Objective-C differs from C++, another object-oriented extension of C, by deferring decisions until runtime that C++ would make at compile time. Objective-C is distinguished by the following key features:
· Dynamic dispatch
· Dynamic typing
· Dynamic loading
1.2.1 Dynamic Dispatch
Object-oriented languages replace function calls with messages. The difference is that the same message may trigger different code at runtime, depending on the type of the message receiver. Objective-C decides dynamically—at runtime—what code will handle a message by searching the receiver's class and parent classes. (The Objective-C runtime caches the search results for better performance.) By contrast, a C++ compiler constructs a dispatch table statically—at compile time.
Because the simple linear search for a receiver used by Objective-C mirrors the way we think about inheritance, it's easy to understand how an Objective-C program works. Dynamic dispatch can handle changes in the inheritance hierarchy at runtime. A dynamic message-sending model is also more natural for distributed objects than a table-based model.
1.2.2 Dynamic Typing
Because message-sending is dynamic, Objective-C lets you send messages to objects whose type has not been declared. The Objective-C environment determines dynamically—at runtime—the class of the message receiver and calls the appropriate code. By comparison, C++ requires the type of the receiver to be declared statically—at compile time—in order to consult dispatch tables.
Static typing allows the compiler to detect some program errors, but type checking is undecidable—that is, no algorithm can infallibly distinguish between programs that have type errors and those that do not. A compiler must either miss some errors or prohibit some safe operations. Of course, in practice compilers follow the latter course, so some programs that would run correctly will not compile. Dynamic typing admits designs whose correctness is not evident to the compiler.
Objective-C lets you use static type checking where you want it, but dynamic typing where you need it. This represents a move away from the question of What is the receiver's type at compile time? to What messages does an object respond to at runtime? Since programs run only at runtime, this is a more useful perspective.
1.2.3 Dynamic Loading
Because the process of method dispatch is simple and uniform, it's easy to defer until runtime the linking of separately-compiled code modules. Objective-C programs can be factored into components that have minimal interdependency; these components can be loaded as needed by a running program. This makes it easier to deliver code, as well as content, over a network; design applications or systems that are distributed; or write an application that can be extended by third-party plug-ins.
1.2.4 Which Objective-C?
If you are programming in a Unix environment, you probably already have an Objective-C compiler: the gcc compiler, which is part of many Unix installations and is available under the terms of the GNU Public License. Because of the wide availability of this compiler for many software and hardware platforms, this handbook documents the features of the language compiled by Version 3.1 of gcc.
Apple Computer has also adopted gcc as the compiler for its OS X platform, which is based on a Unix variant called Darwin. Darwin provides its own Objective-C runtime, and a class library called Cocoa. This handbook notes the differences between the Darwin and GNU runtime environments, and documents the root classes supplied by both GNU and Cocoa.
There is also a class library called GNUstep, distributed under the terms of the GNU Lesser (Library) Public License. GNUstep is an outgrowth of the same code that gave rise to Cocoa, and is largely compatible with Cocoa. Discussions in this book of Cocoa features such as the NSObject root class will apply equally to the GNUstep version.
1.2.5 How Do I Get Started?
Here is a minimal Objective-C program. If you can successfully compile and run it, your Objective-C installation is working correctly.
#import
int main(int argc, char * argv[ ]) {
Object* obj = [Object new];
return 0;
}
To build this program from a shell prompt, save it in a file named myProg.m and issue the following commands. (Your platform may require a different threading library, which is specified in the last parameter. If you are using an integrated development environment, follow its documentation for building a program.)
gcc -c myProg.m
gcc -o myProg myProg.o -lobjc -lpthread
When this little program succeeds in compiling, you are ready to start learning the language and writing Objective-C programs.