Apple - The Objective-C.programming Language
Apple - The Objective-C.programming Language
2010-12-08
OR WRITTEN, EXPRESS OR IMPLIED. No Apple
dealer, agent, or employee is authorized to make
Apple Inc. any modification, extension, or addition to this
© 2010 Apple Inc. warranty.
All rights reserved. Some states do not allow the exclusion or limitation
of implied warranties or liability for incidental or
consequential damages, so the above limitation or
No part of this publication may be reproduced, exclusion may not apply to you. This warranty gives
stored in a retrieval system, or transmitted, in you specific legal rights, and you may also have
other rights which vary from state to state.
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
the documentation contains Apple’s copyright
notice.
The Apple logo is a trademark of Apple Inc.
No licenses, express or implied, are granted
with respect to any of the technology described
in this document. Apple retains all intellectual
property rights associated with the technology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
Introduction Introduction 9
Source Files 35
Class Interface 35
Importing the Interface 37
Referring to Other Classes 37
The Role of the Interface 38
Class Implementation 38
Referring to Instance Variables 39
The Scope of Instance Variables 40
3
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
CONTENTS
Chapter 4 Protocols 59
Overview 69
Property Declaration and Implementation 69
Property Declaration 70
Property Declaration Attributes 70
Property Implementation Directives 73
Using Properties 74
Supported Types 74
Property Redeclaration 75
Copy 75
dealloc 76
Core Foundation 77
4
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
CONTENTS
Chapter 10 Selectors 99
5
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
CONTENTS
Glossary 113
6
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
Figures and Listings
Figure 2-1 The scope of instance variables (@package scope not shown) 41
Figure 2-2 The hierarchy of High, Mid, and Low 44
7
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
FIGURES AND LISTINGS
8
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
INTRODUCTION
Introduction
The Objective-C language is a simple computer language designed to enable sophisticated object-oriented
programming. Objective-C is defined as a small but powerful set of extensions to the standard ANSI C
language. Its additions to C are mostly based on Smalltalk, one of the first object-oriented programming
languages. Objective-C is designed to give C full object-oriented programming capabilities, and to do so in
a simple and straightforward way.
● A library of objects
● A runtime environment
This document is about the first component of the development environment—the programming language.
It fully describes the version of the Objective-C language released in Mac OS X v10.6 and iOS 4.0. This document
also provides a foundation for learning about the second component, the Objective-C application
frameworks—collectively known as Cocoa. The runtime environment is described in a separate document,
Objective-C Runtime Programming Guide.
● Programming in Objective-C
● Finding out about the basis for the Cocoa application frameworks
This document both introduces the object-oriented model that Objective-C is based upon and fully documents
the language. It concentrates on the Objective-C extensions to C, not on the C language itself.
Because this isn’t a document about C, it assumes some prior acquaintance with that language. Object-oriented
programming in Objective-C is, however, sufficiently different from procedural programming in ANSI C that
you won’t be hampered if you’re not an experienced C programmer.
The following chapters cover all the features Objective-C adds to standard C.
A glossary at the end of this document provides definitions of terms specific to Objective-C and object-oriented
programming.
Conventions
This document makes special use of computer voice and italic fonts. Computer voice denotes words or
characters that are to be taken literally (typed as they appear). Italic denotes words that represent something
else or can be varied. For example, the syntax:
@interfaceClassName(CategoryName)
means that @interface and the two parentheses are required, but that you can choose the class name and
category name.
Where example code is shown, ellipsis points indicates the parts, often substantial parts, that have been
omitted:
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
...
}
10 Conventions
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
INTRODUCTION
Introduction
See Also
If you have never used object-oriented programming to create applications, you should read Object-Oriented
Programming with Objective-C. You should also consider reading it if you have used other object-oriented
development environments such as C++ and Java because they have many expectations and conventions
different from those of Objective-C. Object-Oriented Programming with Objective-C is designed to help you
become familiar with object-oriented development from the perspective of an Objective-C developer. It
spells out some of the implications of object-oriented design and gives you a flavor of what writing an
object-oriented program is really like.
Objective-C Runtime Reference describes the data structures and functions of the Objective-C runtime support
library. Your programs can use these interfaces to interact with the Objective-C runtime system. For example,
you can add classes or methods, or obtain a list of all class definitions for loaded classes.
Memory Management
Objective-C supports two mechanisms for memory management: automatic garbage collection and reference
counting:
● Garbage Collection Programming Guide describes the garbage collection system used in Mac OS X. (Not
available for iOS—you cannot access this document through the iOS Dev Center.)
● Memory Management Programming Guide describes the reference counting system used in iOS and
Mac OS X.
See Also 11
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
INTRODUCTION
Introduction
12 See Also
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
CHAPTER 1
This chapter describes the fundamentals of objects, classes, and messaging as used and implemented by the
Objective-C language. It also introduces the Objective-C runtime.
The Objective-C language defers as many decisions as it can from compile time and link time to runtime.
Whenever possible, it dynamically performs operations such as creating objects and determining what
method to invoke. Therefore, the language requires not just a compiler, but also a runtime system to execute
the compiled code. The runtime system acts as a kind of operating system for the Objective-C language; it’s
what makes the language work. Typically, however, you don’t need to interact with the runtime directly. To
understand more about the functionality it offers, though, see Objective-C Runtime Programming Guide.
Objects
As the name implies, object-oriented programs are built around objects. An object associates data with the
particular operations that can use or affect that data. Objective-C provides a data type to identify an object
variable without specifying a particular class of the object.
Object Basics
An object associates data with the particular operations that can use or affect that data. In Objective-C, these
operations are known as the object’s methods; the data they affect are its instance variables (in other
environments they may be referred to as ivars or member variables). In essence, an object bundles a data
structure (instance variables) and a group of procedures (methods) into a self-contained programming unit.
In Objective-C, an object’s instance variables are internal to the object; generally, you get access to an object’s
state only through the object’s methods (you can specify whether subclasses or other objects can access
instance variables directly by using scope directives, see “The Scope of Instance Variables” (page 40)). For
others to find out something about an object, there has to be a method to supply the information. For
example, a rectangle would have methods that reveal its size and position.
Moreover, an object sees only the methods that were designed for it; it can’t mistakenly perform methods
intended for other types of objects. Just as a C function protects its local variables, hiding them from the rest
of the program, an object hides both its instance variables and its method implementations.
id
In Objective-C, object identifiers are of a distinct data type: id. This type is the general type for any kind of
object regardless of class and can be used for instances of a class and for class objects themselves.
id anObject;
For the object-oriented constructs of Objective-C, such as method return values, id replaces int as the
default data type. (For strictly C constructs, such as function return values, int remains the default type.)
The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of
Objective-C are defined in the header file objc/objc.h.
Every object thus has an isa variable that tells it of what class it is an instance. Since the Class type is itself
defined as a pointer:
Dynamic Typing
The id type is completely nonrestrictive. By itself, it yields no information about an object, except that it is
an object. At some point, a program typically needs to find more specific information about the objects it
contains. Since the id type designator can’t supply this specific information to the compiler, each object has
to be able to supply it at runtime.
The isa instance variable identifies the object’s class—what kind of object it is. Objects with the same
behavior (methods) and the same kinds of data (instance variables) are members of the same class.
Objects are thus dynamically typed at runtime. Whenever it needs to, the runtime system can find the exact
class that an object belongs to, just by asking the object. (To learn more about the runtime, see Objective-C
Runtime Programming Guide.) Dynamic typing in Objective-C serves as the foundation for dynamic binding,
discussed later.
The isa variable also enables objects to perform introspection—to find out about themselves (or other
objects). The compiler records information about class definitions in data structures for the runtime system
to use. The functions of the runtime system use isa to find this information at runtime. Using the runtime
system, you can, for example, determine whether an object implements a particular method or discover the
name of its superclass.
Object classes are discussed in more detail under “Classes” (page 23).
It’s also possible to give the compiler information about the class of an object by statically typing it in source
code using the class name. Classes are particular kinds of objects, and the class name can serve as a type
name. See “Class Types” (page 26) and “Enabling Static Behavior” (page 95).
14 Objects
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
Memory Management
In any program, it is important to ensure that objects are deallocated when they are no longer
needed—otherwise your application’s memory footprint becomes larger than necessary. It is also important
to ensure that you do not deallocate objects while they’re still being used.
Objective-C offers two mechanisms for memory management that allow you to meet these goals:
● Reference counting, where you are ultimately responsible for determining the lifetime of objects.
● Garbage collection, where you pass responsibility for determining the lifetime of objects to an automatic
“collector.”
Garbage collection is described in Garbage Collection Programming Guide. (Not available for iOS—you
cannot access this document through the iOS Dev Center.)
Object Messaging
This section explains the syntax of sending messages, including how you can nest message expressions. It
also discusses the scope or “visibility” of an object’s instance variables, and the concepts of polymorphism
and dynamic binding.
Message Syntax
To get an object to do something, you send it a message telling it to apply a method. In Objective-C, message
expressions are enclosed in brackets:
[receiver message]
The receiver is an object, and the message tells it what to do. In source code, the message is simply the name
of a method and any parameters that are passed to it. When a message is sent, the runtime system selects
the appropriate method from the receiver’s repertoire and invokes it.
For example, this message tells the myRectangle object to perform its display method, which causes the
rectangle to display itself:
[myRectangle display];
Because the method name in a message serves to “select” a method implementation, method names in
messages are often referred to as selectors.
Methods can also take parameters, sometimes called arguments. A message with a single parameter affixes
a colon (:) to the name and puts the parameter right after the colon:
[myRectangle setWidth:20.0];
Object Messaging 15
2010-12-08 | © 2010 Apple Inc. All Rights Reserved.