The Objective-C Programming Language

Inside Mac OS X

The Objective-C Programming

Language

February 2003

? Apple Computer, Inc.

? 2002 Apple Computer, Inc.

All rights reserved.

No part of this publication may be

reproduced, stored in a retrieval

system, or transmitted, in any form or

by any means, mechanical, electronic,

photocopying, recording, or

otherwise, without prior written

permission of Apple Computer, 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 Computer, Inc.

Use of the ¡°keyboard¡± Apple logo

(Option-Shift-K) for commercial

purposes without the prior written

consent of Apple may constitute

trademark infringement and unfair

competition in violation of federal

and state laws.

No licenses, express or implied, are

granted with respect to any of the

technology described in this book.

Apple retains all intellectual property

rights associated with the technology

described in this book. This book is

intended to assist application

developers to develop applications

only for Apple-labeled or

Apple-licensed computers.

Every effort has been made to ensure

that the information in this document

is accurate. Apple is not responsible

for typographical errors.

Apple Computer, Inc.

1 Infinite Loop

Cupertino, CA 95014

408-996-1010

Apple, the Apple logo, and

Macintosh are trademarks of Apple

Computer, Inc., registered in the

United States and other countries.

HotSpot is a trademark of Sun

Microsystems, Inc.

Simultaneously published in the

United States and Canada.

Even though Apple has reviewed this

manual, APPLE MAKES NO

WARRANTY OR REPRESENTATION,

EITHER EXPRESS OR IMPLIED, WITH

RESPECT TO THIS MANUAL, ITS

QUALITY, ACCURACY,

MERCHANTABILITY, OR FITNESS

FOR A PARTICULAR PURPOSE. AS A

RESULT, THIS MANUAL IS SOLD ¡°AS

IS,¡± AND YOU, THE PURCHASER, ARE

ASSUMING THE ENTIRE RISK AS TO

ITS QUALITY AND ACCURACY.

IN NO EVENT WILL APPLE BE LIABLE

FOR DIRECT, INDIRECT, SPECIAL,

INCIDENTAL, OR CONSEQUENTIAL

DAMAGES RESULTING FROM ANY

DEFECT OR INACCURACY IN THIS

MANUAL, even if advised of the

possibility of such damages.

THE WARRANTY AND REMEDIES SET

FORTH ABOVE ARE EXCLUSIVE AND

IN LIEU OF ALL OTHERS, ORAL OR

WRITTEN, EXPRESS OR IMPLIED. No

Apple dealer, agent, or employee is

authorized to make any modification,

extension, or addition to this warranty.

Some states do not allow the exclusion or

limitation of implied warranties or

liability for incidental or consequential

damages, so the above limitation or

exclusion may not apply to you. This

warranty gives you specific legal rights,

and you may also have other rights which

vary from state to state.

Contents

Figures, Listings, and Tables

Chapter 1

11

Introduction

The Development Environment

14

Why Objective-C

15

How This Book is Organized

16

Conventions

17

Chapter 2

Object-Oriented Programming

Interface and Implementation

20

The Object Model

24

The Messaging Metaphor

26

Classes

28

Modularity

29

Reusability

30

Mechanisms Of Abstraction

31

Encapsulation

32

Polymorphism

33

Inheritance

35

Class Hierarchies

35

Subclass Definitions

36

Uses of Inheritance

37

Dynamism

39

Dynamic Typing

39

Dynamic Binding

40

Dynamic Loading

43

Structuring Programs

44

Outlet Connections

45

3

? Apple Computer, Inc. February 2003

C O N T E N T S

Extrinsic and Intrinsic Connections

47

Activating the Object Network

47

Aggregation and Decomposition

48

Models and Frameworks

49

Structuring the Programming Task

50

Collaboration

51

Organizing Object-Oriented Projects

52

Designing on a Large Scale

52

Separating the Interface from the Implementation

Modularizing the Work

52

Keeping the Interface Simple

53

Making Decisions Dynamically

53

Inheriting Generic Code

53

Reusing Tested Code

54

Chapter 3

The Objective-C Language

Objective-C Objects

55

id

56

Dynamic Typing

57

Object Messaging

58

The Receiver¡¯s Instance Variables

59

Polymorphism

60

Dynamic Binding

60

Classes

62

Inheritance

62

The NSObject Class

64

Inheriting Instance Variables

64

Inheriting Methods

65

Overriding One Method With Another

Abstract Classes

66

Class Types

67

Static Typing

67

Type Introspection

68

Class Objects

68

Creating Instances

70

Customization With Class Objects

71

4

? Apple Computer, Inc. February 2003

66

52

C O N T E N T S

Variables and Class Objects

72

Initializing a Class Object

73

Methods of the Root Class

74

Class Names in Source Code

74

Defining a Class

75

The Interface

76

Importing the Interface

78

Referring to Other Classes

79

The Role of the Interface

79

The Implementation

80

Referring to Instance Variables

82

The Scope of Instance Variables

83

How Messaging Works

87

Selectors

90

Methods and Selectors

91

Method Return and Argument Types

91

Varying the Message at Runtime

92

The Target-Action Paradigm

92

Avoiding Messaging Errors

94

Hidden Arguments

94

Messages to self and super

95

An Example

96

Using super

99

Redefining self

99

Extending Classes

101

Categories¡ªAdding Methods to Existing Classes

101

Adding to a Class

102

How Categories Are Used

103

Categories of the Root Class

103

Protocols¡ªDeclaring Interfaces for Others to Implement

When to Use Protocols

105

Enabling Static Behaviors

115

Static Typing

116

Type Checking

117

Return and Argument Types

118

Static Typing to an Inherited Class

118

Getting a Method Address

120

Getting an Object Data Structure

120

104

5

? Apple Computer, Inc. February 2003

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download