The Objective-C language is a simple computer language designed to enable sophisticated object-oriented
programming. Objective-C extends the standard ANSI C language by providing syntax for defining classes, and
methods, as well as other constructs that promote dynamic extension of classes. (This document doesn’t
attempt to teach any aspects of C itself. If you’re not familiar with the language, you should learn about the
basics before you proceed.)
If you have programmed with object-oriented languages before, the following information should help you learn
the basic syntax of Objective-C. Many of the traditional object-oriented concepts, such as encapsulation,
inheritance, and polymorphism, are all present in Objective-C. There are a few important differences, but those
differences are called out in this article and more detailed information is available if you need it.
If you have never programmed using an object-oriented language before, you need to have at least a basic
understanding of the associated concepts before proceeding. The use of objects and object-oriented design
patterns is fundamental to the design of Cocoa applications, and understanding how they interact is critical to
creating your applications. For an overview of concepts, see Object-Oriented Programming with Objective-C. In
addition, see Cocoa Fundamentals Guide for information about the design patterns used in Cocoa.
For full details of the Objective-C language and syntax, see The Objective-C Programming Language.
Objective-C: A Superset of C
Objective-C is a superset of the ANSI version of the C programming language and supports the same basic
syntax as C. As with C code, you define header files and source files to separate public declarations from the
implementation details of your code. Objective-C header files use the file extensions listed in Table 1.
Table 1 File extensions for Objective-C code
Extension Source type
Header files. Header files contain class, type, function, and constant declarations.
Source files. This is the typical extension used for source files and can contain both Objective-C
and C code.
Source files. A source file with this extension can contain C++ code in addition to Objective-C
and C code. This extension should be used only if you actually refer to C++ classes or features
from your Objective-C code.
When you want to include header files in your source code, you typically use a directive. This is like
, except that it makes sure that the same file is never included more than once. The Objective-C
samples and documentation all prefer the use of , and your own code should too.
Classes
As in most other object-oriented languages, classes in Objective-C provide the basic construct for encapsulating
some data with the actions that operate on that data. An object is a runtime instance of a class, and contains its
own in-memory copy of the instance variables declared by that class and pointers to the methods of the class.
The specification of a class in Objective-C requires two distinct pieces: the interface and the implementation. The
interface portion contains the class declaration and defines the instance variables and methods associated with
the class. The interface is usually in a file. The implementation portion contains the actual code for the
methods of the class. The implementation is usually in a file.
Figure 1 shows the syntax for declaring a class called , which inherits from the base class. The
class declaration always begins with the compiler directive and ends with the directive. Following
the class name (and separated from it by a colon) is the name of the parent class. The instance (or member)
variables of the class are declared in a code block that is delineated by braces ( and ). Following the instance
variable block is the list of methods declared by the class. A semicolon character marks the end of each instance
variable and method declaration.
Figure 1 A class declaration
Learning Objective-C: A Primer
Learning Objective-C: A Primer
iPhone Dev Center: Learning Objective-C: A Primer http://developer.apple.com/iphone/library/referencelibrary/GettingS...
1 of 5 2010/01/11 16:27