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
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition By Stephen G. Kochan ISBN:
9780321605559 Publisher: Addison-Wesley Professional
Prepared for David Mease, Safari ID: dave@mease.com
Print Publication Date: 2008/12/29 User number: 202380
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use
requires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.
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
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition By Stephen G. Kochan ISBN:
9780321605559 Publisher: Addison-Wesley Professional
Prepared for David Mease, Safari ID: dave@mease.com
Print Publication Date: 2008/12/29 User number: 202380
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use
requires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.
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
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition By Stephen G. Kochan ISBN:
9780321605559 Publisher: Addison-Wesley Professional
Prepared for David Mease, Safari ID: dave@mease.com
Print Publication Date: 2008/12/29 User number: 202380
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use
requires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.
Programming in
Objective-C 2.0
Stephen G. Kochan
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York
• Toronto • Montreal • London • Munich • Paris • Madrid
Cape Town
• Sydney • Tokyo • Singapore • Mexico City
Programming in Objective-C 2.0, Second Edition Page 1 Return to Table of Contents
Programming in Objective-C 2.0, Second Edition
Programming in Objective-C 2.0, Second Edition By Stephen G. Kochan ISBN:
9780321605559 Publisher: Addison-Wesley Professional
Prepared for David Mease, Safari ID: dave@mease.com
Print Publication Date: 2008/12/29 User number: 202380
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use
requires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.