Programming in Objective-C, Sixth Edition Updated for OS X 10.9 Mavericks, iOS 7, and Xcode 5 Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apples iOS and OS X platforms. Table of Contents......Page 9 1 Introduction......Page 20 What You Will Learn from This Book......Page 21 How This Book Is Organized......Page 22 Acknowledgments......Page 24 Preface to the Sixth Edition......Page 25 Compiling and Running Programs......Page 26 Explanation of Your First Program......Page 37 Displaying the Values of Variables......Page 41 Exercises......Page 44 What Is an Object, Anyway?......Page 46 Instances and Methods......Page 47 An Objective-C Class for Working with Fractions......Page 49 The @interface Section......Page 52 The @implementation Section......Page 56 The program Section......Page 58 Accessing Instance Variables and Data Encapsulation......Page 64 Exercises......Page 68 Data Types and Constants......Page 70 Arithmetic Expressions......Page 74 Assignment Operators......Page 83 A Calculator Class......Page 84 Exercises......Page 86 5 Program Looping......Page 90 The for Statement......Page 91 The while Statement......Page 103 The do Statement......Page 108 Summary......Page 110 Exercises......Page 111 The if Statement......Page 112 The switch Statement......Page 134 Boolean Variables......Page 137 The Conditional Operator......Page 142 Exercises......Page 144 Separate Interface and Implementation Files......Page 146 Synthesized Accessor Methods......Page 152 Accessing Properties Using the Dot Operator......Page 154 Multiple Arguments to Methods......Page 156 Local Variables......Page 162 The self Keyword......Page 167 Allocating and Returning Objects from Methods......Page 168 Exercises......Page 170 It All Begins at the Root......Page 172 Extension through Inheritance: Adding New Methods......Page 177 Overriding Methods......Page 190 Exercises......Page 195 Polymorphism: Same Name, Different Class......Page 198 Dynamic Binding and the id Type......Page 201 Compile Time Versus Runtime Checking......Page 203 The id Data Type and Static Typing......Page 204 Asking Questions about Classes......Page 206 Exception Handling Using @try......Page 211 Exercises......Page 214 Initializing Objects......Page 216 Scope Revisited......Page 219 Enumerated Data Types......Page 226 The typedef Statement......Page 229 Data Type Conversions......Page 230 Bit Operators......Page 232 Exercises......Page 239 Categories......Page 242 Class Extensions......Page 247 Protocols and Delegation......Page 249 Composite Objects......Page 253 Exercises......Page 254 The #define Statement......Page 256 The #import Statement......Page 263 Conditional Compilation......Page 264 Exercises......Page 268 13 Underlying C Language Features......Page 270 Arrays......Page 271 Functions......Page 277 Blocks......Page 285 Structures......Page 289 Pointers......Page 296 Miscellaneous Language Features......Page 316 How Things Work......Page 321 Exercises......Page 323 Foundation Documentation......Page 326 Number Objects......Page 330 String Objects......Page 336 Array Objects......Page 352 Dictionary Objects......Page 381 Set Objects......Page 386 Exercises......Page 392 16 Working with Files......Page 396 Managing Files and Directories: NSFileManager......Page 397 Working with Paths: NSPathUtilities.h......Page 408 Basic File Operations: NSFileHandle......Page 417 The NSURL Class......Page 422 The NSBundle Class......Page 423 Exercises......Page 424 17 Memory Management and Automatic Reference Counting......Page 426 Manual Reference Counting......Page 428 The Event Loop and Memory Allocation......Page 431 Summary of Manual Memory Management Rules......Page 433 Strong Variables......Page 434 Weak Variables......Page 435 @autoreleasepool Blocks......Page 436 Method Names and Non-ARC Compiled Code......Page 437 The copy and mutableCopy Methods......Page 438 Shallow Versus Deep Copying......Page 441 Implementing the Protocol......Page 443 Copying Objects in Setter and Getter Methods......Page 446 Exercises......Page 448 Archiving with XML Property Lists......Page 450 Archiving with NSKeyedArchiver......Page 453 Writing Encoding and Decoding Methods......Page 454 Using NSData to Create Custom Archives......Page 461 Using the Archiver to Copy Objects......Page 465 Exercises......Page 466 Framework Layers......Page 468 Cocoa Touch......Page 469 Your First iPhone Application......Page 472 An iPhone Fraction Calculator......Page 488 Summary......Page 502 Exercises......Page 503 A: Glossary......Page 504 B: Address Book Example Source Code......Page 512 Index......Page 518 A......Page 519 B......Page 521 C......Page 522 D......Page 526 F......Page 528 H......Page 532 I......Page 533 L......Page 535 M......Page 536 N......Page 541 O......Page 542 P......Page 544 R......Page 545 S......Page 546 U......Page 549 X-Y-Z......Page 550 Table of Contents 9 1 Introduction 20 What You Will Learn from This Book 21 How This Book Is Organized 22 Support 24 Acknowledgments 24 Preface to the Sixth Edition 25 I: The Objective-C Language 26 2 Programming in Objective-C 26 Compiling and Running Programs 26 Explanation of Your First Program 37 Displaying the Values of Variables 41 Summary 44 Exercises 44 3 Classes, Objects, and Methods 46 What Is an Object, Anyway? 46 Instances and Methods 47 An Objective-C Class for Working with Fractions 49 The @interface Section 52 The @implementation Section 56 The program Section 58 Accessing Instance Variables and Data Encapsulation 64 Summary 68 Exercises 68 4 Data Types and Expressions 70 Data Types and Constants 70 Arithmetic Expressions 74 Assignment Operators 83 A Calculator Class 84 Exercises 86 5 Program Looping 90 The for Statement 91 The while Statement 103 The do Statement 108 The break Statement 110 The continue Statement 110 Summary 110 Exercises 111 6 Making Decisions 112 The if Statement 112 The switch Statement 134 Boolean Variables 137 The Conditional Operator 142 Exercises 144 7 More on Classes 146 Separate Interface and Implementation Files 146 Synthesized Accessor Methods 152 Accessing Properties Using the Dot Operator 154 Multiple Arguments to Methods 156 Local Variables 162 The self Keyword 167 Allocating and Returning Objects from Methods 168 Exercises 170 8 Inheritance 172 It All Begins at the Root 172 Extension through Inheritance: Adding New Methods 177 Overriding Methods 190 Abstract Classes 195 Exercises 195 9 Polymorphism, Dynamic Typing, and Dynamic Binding 198 Polymorphism: Same Name, Different Class 198 Dynamic Binding and the id Type 201 Compile Time Versus Runtime Checking 203 The id Data Type and Static Typing 204 Asking Questions about Classes 206 Exception Handling Using @try 211 Exercises 214 10 More on Variables and Data Types 216 Initializing Objects 216 Scope Revisited 219 Enumerated Data Types 226 The typedef Statement 229 Data Type Conversions 230 Bit Operators 232 Exercises 239 11 Categories and Protocols 242 Categories 242 Class Extensions 247 Protocols and Delegation 249 Composite Objects 253 Exercises 254 12 The Preprocessor 256 The #define Statement 256 The #import Statement 263 Conditional Compilation 264 Exercises 268 13 Underlying C Language Features 270 Arrays 271 Functions 277 Blocks 285 Structures 289 Pointers 296 They鈥檙e Not Objects! 316 Miscellaneous Language Features 316 How Things Work 321 Exercises 323 II: The Foundation Framework 326 14 Introduction to the Foundation Framework 326 Foundation Documentation 326 15 Numbers, Strings, and Collections 330 Number Objects 330 String Objects 336 Array Objects 352 Dictionary Objects 381 Set Objects 386 Exercises 392 16 Working with Files 396 Managing Files and Directories: NSFileManager 397 Working with Paths: NSPathUtilities.h 408 Basic File Operations: NSFileHandle 417 The NSURL Class 422 The NSBundle Class 423 Exercises 424 17 Memory Management and Automatic Reference Counting 426 Automatic Garbage Collection 428 Manual Reference Counting 428 The Event Loop and Memory Allocation 431 Summary of Manual Memory Management Rules 433 Automatic Reference Counting 434 Strong Variables 434 Weak Variables 435 @autoreleasepool Blocks 436 Method Names and Non-ARC Compiled Code 437 18 Copying Objects 438 The copy and mutableCopy Methods 438 Shallow Versus Deep Copying 441 Implementing the Protocol 443 Copying Objects in Setter and Getter Methods 446 Exercises 448 19 Archiving 450 Archiving with XML Property Lists 450 Archiving with NSKeyedArchiver 453 Writing Encoding and Decoding Methods 454 Using NSData to Create Custom Archives 461 Using the Archiver to Copy Objects 465 Exercises 466 III: Cocoa, Cocoa Touch, and the iOS SDK 468 20 Introduction to Cocoa and Cocoa Touch 468 Framework Layers 468 Cocoa Touch 469 21 Writing iOS Applications 472 The iOS SDK 472 Your First iPhone Application 472 An iPhone Fraction Calculator 488 Summary 502 Exercises 503 Appendixes 504 A: Glossary 504 B: Address Book Example Source Code 512 Index 518 A 519 B 521 C 522 D 526 E 528 F 528 G 532 H 532 I 533 J-K 535 L 535 M 536 N 541 O 542 P 544 Q 545 R 545 S 546 T 549 U 549 V 550 W 550 X-Y-Z 550 Annotation Updated for OS X 10.9 Mavericks, iOS 7, and Xcode 5 Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple's iOS and OS X platforms. The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C. Readers can also learn the concepts of object-oriented programming without having to first learn all of the intricacies of the underlying C programming language. This unique approach to learning, combined with many small program examples and exercises at the end of each chapter, makes Programming in Objective-C ideally suited for either classroom use or self-study. This edition has been fully updated to incorporate new Objective-C features and technologies introduced with Xcode 5, iOS 7, and Mac OS X Mavericks. "The best book on any programming language that I've ever read. If you want to learn Objective-C, buy it."--Calvin Wolcott "An excellent resource for a new programmer who wants to learn Objective-C as their first programming language--a woefully underserved market."--Pat Hughes This unique approach to learning, combined with many small program examples and exercises at the end of each chapter, makes Programming in Objective-C ideally suited for either classroom use or self-study. This edition has been fully updated to incorporate new Objective-C features and technologies introduced with Xcode 5, iOS 7, and Mac OS X Mavericks. -Publishers description.