Programming in Objective-C 2.0
Copyright © 2009 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect to
the use of the information contained herein. Although every precaution has been taken in
the preparation of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of the informa-
tion contained herein.
ISBN-13: 978-0-321-56615-7
ISBN-10: 0-321-56615-7
Library of Congress Cataloging-in-Publication Data:
Kochan, Stephen G.
Programming in Objective-C 2.0 / Stephen G. Kochan. -- 2nd ed.
p. cm.
ISBN 978-0-321-56615-7 (pbk.)
1. Objective-C (Computer program language) 2. Object-oriented
programming (Computer science) 3. Macintosh (Computer)--Programming.
I. Title.
QA76.73.O115K63 2009
005.1'17--dc22
2008049771
Printed in the United States of America
First Printing December 2008
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor-
mation. Use of a term in this book should not be regarded as affecting the validity of any
trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible,
but no warranty or fitness is implied. The information provided is on an “as is” basis. The
author and the publisher shall have neither liability nor responsibility to any person or entity
with respect to any loss or damages arising from the information contained in this book.
Bulk Sales
Pearson offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearsoned.com
Acquisitions Editor
Mark Taber
Development
Editor
Michael Thurston
Managing Editor
Patrick Kanouse
Project Editor
Mandie Frank
Copy Editor
Krista Hansing
Editorial Services,
Inc.
Indexer
Ken Johnson
Proofreader
Arle Writing
and Editing
Technical Editor
Michael Trent
Publishing
Coordinator
Vanessa Evans
Designer
Gary Adair
Compositor
Mark Shirar
Table of Contents
Copyright..................................................................................................... 1
Developer’s Library
..................................................................................... 4
About the Author......................................................................................... 5
About the Technical Reviewers................................................................... 5
We Want to Hear from You!........................................................................ 6
Reader Services.......................................................................................... 6
Chapter 1. Introduction............................................................................... 8
What You Will Learn from This Book............................................................................................................................................ 9
How This Book Is Organized......................................................................................................................................................... 10
Acknowledgments......................................................................................................................................................................... 12
Part I: The Objective-C 2.0 Language......................................................... 14
Chapter 2. Programming in Objective-C....................................................................................................................................... 16
Compiling and Running Programs............................................................................................................................................ 16
Explanation of Your First Program........................................................................................................................................... 25
Displaying the Values of Variables............................................................................................................................................ 29
Summary................................................................................................................................................................................... 32
Exercises.................................................................................................................................................................................... 32
Chapter 3. Classes, Objects, and Methods.................................................................................................................................... 34
What Is an Object, Anyway?..................................................................................................................................................... 34
Instances and Methods............................................................................................................................................................. 35
An Objective-C Class for Working with Fractions.................................................................................................................... 37
The @interface Section............................................................................................................................................................. 40
The @implementation Section................................................................................................................................................. 44
The program Section................................................................................................................................................................. 45
Accessing Instance Variables and Data Encapsulation............................................................................................................. 51
Summary.................................................................................................................................................................................... 54
Exercises.................................................................................................................................................................................... 54
Chapter 4. Data Types and Expressions....................................................................................................................................... 56
Data Types and Constants......................................................................................................................................................... 56
Arithmetic Expressions............................................................................................................................................................. 63
Assignment Operators............................................................................................................................................................... 71
A Calculator Class...................................................................................................................................................................... 72
Bit Operators............................................................................................................................................................................. 74
Types: _Bool, _Complex, and _Imaginary.............................................................................................................................. 80
Exercises.................................................................................................................................................................................... 80
Chapter 5. Program Looping........................................................................................................................................................ 84
The for Statement...................................................................................................................................................................... 85
The while Statement.................................................................................................................................................................. 96
The do Statement..................................................................................................................................................................... 101
The break Statement............................................................................................................................................................... 102
The continue Statement.......................................................................................................................................................... 103
Summary.................................................................................................................................................................................. 103
Exercises.................................................................................................................................................................................. 103
Chapter 6. Making Decisions...................................................................................................................................................... 106
The if Statement...................................................................................................................................................................... 106
The switch Statement............................................................................................................................................................... 127
Boolean Variables.................................................................................................................................................................... 130
The Conditional Operator........................................................................................................................................................ 135
Exercises.................................................................................................................................................................................. 136
Chapter 7. More on Classes......................................................................................................................................................... 140
Separate Interface and Implementation Files........................................................................................................................ 140
Synthesized Accessor Methods................................................................................................................................................ 146
Accessing Properties Using the Dot Operator......................................................................................................................... 147
Multiple Arguments to Methods............................................................................................................................................. 148
Local Variables......................................................................................................................................................................... 153
The self Keyword...................................................................................................................................................................... 156
Allocating and Returning Objects from Methods................................................................................................................... 157
Exercises.................................................................................................................................................................................. 163
Chapter 8. Inheritance................................................................................................................................................................ 164
It All Begins at the Root........................................................................................................................................................... 164
Extension Through Inheritance: Adding New Methods......................................................................................................... 169
Overriding Methods
................................................................................................................................................................. 182
Extension Through Inheritance: Adding New Instance Variables......................................................................................... 188
Abstract Classes....................................................................................................................................................................... 190
Exercises................................................................................................................................................................................... 191
Chapter 9. Polymorphism, Dynamic Typing, and Dynamic Binding......................................................................................... 194
Polymorphism: Same Name, Different Class.......................................................................................................................... 194
Dynamic Binding and the id Type........................................................................................................................................... 198
Compile Time Versus Runtime Checking............................................................................................................................... 200
The id Data Type and Static Typing........................................................................................................................................ 201
Asking Questions About Classes............................................................................................................................................. 202
Exception Handling Using @try............................................................................................................................................. 207
Exercises.................................................................................................................................................................................. 210
Chapter 10. More on Variables and Data Types......................................................................................................................... 212
Initializing Classes................................................................................................................................................................... 212
Scope Revisited........................................................................................................................................................................ 214
Storage Class Specifiers.......................................................................................................................................................... 220
Enumerated Data Types.......................................................................................................................................................... 222
The typedef Statement............................................................................................................................................................ 225
Data Type Conversions............................................................................................................................................................ 227
Exercises.................................................................................................................................................................................. 229
Chapter 11. Categories and Protocols......................................................................................................................................... 232
Categories................................................................................................................................................................................ 232
Protocols.................................................................................................................................................................................. 238
Composite Objects................................................................................................................................................................... 242
Exercises.................................................................................................................................................................................. 243
Chapter 12. The Preprocessor..................................................................................................................................................... 246
The #define Statement............................................................................................................................................................ 246
The #import Statement........................................................................................................................................................... 254
Conditional Compilation......................................................................................................................................................... 257
Exercises.................................................................................................................................................................................. 260
Chapter 13. Underlying C Language Features............................................................................................................................ 262
Arrays...................................................................................................................................................................................... 263
Functions................................................................................................................................................................................. 269
Structures................................................................................................................................................................................ 278
Pointers................................................................................................................................................................................... 290
Unions..................................................................................................................................................................................... 309
They’re Not Objects!................................................................................................................................................................ 312
Miscellaneous Language Features........................................................................................................................................... 312
How Things Work.................................................................................................................................................................... 317
Exercises.................................................................................................................................................................................. 319
Part II: The Foundation Framework........................................................ 322
Chapter 14. Introduction to the Foundation Framework.......................................................................................................... 324
Foundation Documentation.................................................................................................................................................... 324
Chapter 15. Numbers, Strings, and Collections.......................................................................................................................... 328
Number Objects....................................................................................................................................................................... 329
String Objects.......................................................................................................................................................................... 333
Array Objects........................................................................................................................................................................... 348
Synthesized AddressCard Methods........................................................................................................................................ 356
Dictionary Objects................................................................................................................................................................... 374
Set Objects............................................................................................................................................................................... 377
Exercises.................................................................................................................................................................................. 382
Chapter 16. Working with Files.................................................................................................................................................. 384
Managing Files and Directories: NSFileManager.................................................................................................................. 385
Working with Paths: NSPathUtilities.h.................................................................................................................................. 396
Basic File Operations: NSFileHandle..................................................................................................................................... 404
Exercises.................................................................................................................................................................................. 409
Chapter 17. Memory Management.............................................................................................................................................. 412
The Autorelease Pool............................................................................................................................................................... 412
Reference Counting................................................................................................................................................................. 413
An Autorelease Example......................................................................................................................................................... 425
Summary of Memory-Management Rules............................................................................................................................. 426
Garbage Collection.................................................................................................................................................................. 427
Exercises.................................................................................................................................................................................. 429
Chapter 18. Copying Objects...................................................................................................................................................... 430
The copy and mutableCopy Methods...................................................................................................................................... 431
Shallow Versus Deep Copying
................................................................................................................................................. 433
Implementing the <NSCopying> Protocol............................................................................................................................. 436
Copying Objects in Setter and Getter Methods...................................................................................................................... 439
Exercises.................................................................................................................................................................................. 441
Chapter 19. Archiving................................................................................................................................................................. 442
Archiving with XML Property Lists........................................................................................................................................ 442
Archiving with NSKeyedArchiver........................................................................................................................................... 444
Writing Encoding and Decoding Methods............................................................................................................................. 447
Using NSData to Create Custom Archives.............................................................................................................................. 454
Using the Archiver to Copy Objects........................................................................................................................................ 457
Exercises.................................................................................................................................................................................. 459
Part III: Cocoa and the iPhone SDK........................................................ 460
Chapter 20. Introduction to Cocoa............................................................................................................................................. 462
Framework Layers................................................................................................................................................................... 462
Cocoa Touch............................................................................................................................................................................ 463
Chapter 21. Writing iPhone Applications................................................................................................................................... 466
The iPhone SDK...................................................................................................................................................................... 466
Your First iPhone Application................................................................................................................................................ 466
An iPhone Fraction Calculator................................................................................................................................................ 483
Summary................................................................................................................................................................................. 498
Exercises.................................................................................................................................................................................. 499
Part IV: Appendixes................................................................................ 502
Glossary....................................................................................................................................................................................... 504
Appendix B. Objective-C 2.0 Language Summary...................................................................................................................... 512
Digraphs and Identifiers.......................................................................................................................................................... 512
Comments................................................................................................................................................................................ 516
Constants.................................................................................................................................................................................. 517
Data Types and Declarations.................................................................................................................................................. 520
Expressions.............................................................................................................................................................................. 531
Storage Classes and Scope...................................................................................................................................................... 546
Functions................................................................................................................................................................................. 550
Classes...................................................................................................................................................................................... 553
Statements............................................................................................................................................................................... 563
Exception Handling................................................................................................................................................................ 568
Preprocessor............................................................................................................................................................................ 568
Appendix C. Address Book Source Code.................................................................................................................................... 576
AddressCard Interface File...................................................................................................................................................... 576
AddressBook Interface File..................................................................................................................................................... 577
AddressCard Implementation File.......................................................................................................................................... 577
AddressBook Implementation File......................................................................................................................................... 579
Appendix D. Resources............................................................................................................................................................... 582
Answers to Exercises, Errata, and Such................................................................................................................................. 582
Objective-C Language............................................................................................................................................................. 582
C Programming Language....................................................................................................................................................... 583
Cocoa....................................................................................................................................................................................... 583
iPhone and iTouch Application Development........................................................................................................................ 584