HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Full-color figures and code appear as they do in Xcode 5.
In just 24 sessions of one hour or less, you can master the Objective-C language and start using it to write powerful native applications for even the newest Macs and iOS devices! Using this book’s straightforward, step-by-step approach, you’ll get comfortable with Objective-C’s unique capabilities and Apple’s Xcode 5 development environment…make the most of Objective-C objects and messaging…work effectively with design patterns, collections, blocks, Foundation Classes, threading, Git…and a whole lot more. Every lesson builds on what you’ve already learned, giving you a rock-solid foundation for real-world success!
Step-by-Step Instructions carefully walk you through the most common Objective-C development tasks.
Quizzes and Exercises at the end of each chapter help you test your knowledge.
Notes present information related to the discussion.
Tips offer advice or show you easier ways to perform tasks.
Cautions alert you to possible problems and give you advice on how to avoid them.
• Use Xcode 5 to write modern Objective-C software more quickly and efficiently
• Master Objective-C’s object-oriented features and techniques
• Manage projects more efficiently with the Git source code repository
• Write more dynamic code with Objective-C’s powerful messaging architecture
• Declare classes, instance variables, properties, methods, and actions
• Work with mutable and immutable data types
• Organize data with collections, including arrays, dictionaries, and sets
• Painlessly manage memory with Automatic Reference Counting (ARC)
• Expand and extend classes with protocols, delegates, categories, and extensions
• Get started with Apple’s powerful classes and frameworks
• Create and work with code blocks
• Manage queues and threading with Grand Central Dispatch
Using Object-Oriented Features in Objective-C
Download the sample pages (includes Chapter 3 and Index)
Introduction 1
Who Should Read This Book 1
What This Book Covers 1
Downloading the Sample Files 2
How This Book Is Organized 2
PART I: GETTING STARTED WITH OBJECTIVE-C
HOUR 1: Overview of the Developer Program 7
Introducing Objective-C 7
Enrolling as an Apple Developer 8
Choosing Your Program 8
Selecting Your Membership Category 9
Registering for Your Apple ID 10
Setting Up the Development Environment 11
Talking Xcode 11
Introducing the Xcode Workspace 12
Getting Up and Running with Your First Project 16
Summary 17
Q&A 18
Workshop 18
Quiz 18
Quiz Answers 18
Activities 19
HOUR 2: Object-Oriented Programming with Objective-C 21
Object-Oriented Programming in the Objective-C World 21
Implementing Object-Oriented Programming 21
Building Object-Oriented Projects 22
Looking at the Frameworks 23
Creating C with Objects 24
Start Thinking in Objective-C 24
Understanding Data Abstraction 24
Considering Classes and Instances 25
Exploring Encapsulation 25
Using Accessors to Manage Encapsulation 28
Managing Inheritance in the Objective-C World 29
Summary 30
Q&A 31
Workshop 31
Quiz 31
Quiz Answers 31
Activities 31
HOUR 3: Using Object-Oriented Features in Objective-C 33
Communicating to Methods with Messages 33
Looking at a Simple Message 33
Declaring a Method 34
Using Class Methods 35
Working with id—Strongly and Weakly Typed Variables 35
Nesting Messages 36
Looking at Method Signatures and Parameters 36
Allocating and Initializing Objects 37
Summary 39
Q&A 39
Workshop 39
Quiz 39
Quiz Answers 39
Activities 39
HOUR 4: Using Xcode 5 41
Getting to Work with Xcode 41
Keeping Track of Your Source Code 46
Exploring Source Code Control 47
Working in a Source Code Repository World 48
Using Git with Xcode 50
Using a Remote Repository 58
Summary 61
Q&A 61
Workshop 61
Quiz 61
Quiz Answers 61
Activities 62
HOUR 5: Using Compiler Directives 63
Exploring Your Projects 63
Looking at the iOS Project 64
Looking at the OS X Project 64
Looking at Both Projects 65
Working with Compiler Directives 66
Working with Basic Directives 66
Looking at Prefix Headers 67
Looking at Plain C Code in main.m 69
Investigating Header (.h) Files 70
Looking Inside Message (.m) Files 71
Using Objective-C Compiler Directives 74
Summary 74
Q&A 74
Workshop 75
Quiz 75
Quiz Answers 75
Activities 75
PART II: WORKING WITH THE OBJECTIVE-C BASICS
HOUR 6: Exploring Messaging and a Testbed App 77
Setting Up the Testbed Apps 77
Adding a Text Field and Connecting It to Your Code 81
Adding the Text Field 82
Connecting the Text Field to the Code 88
Sending a Message to the Text Field 92
Reviewing the Message Syntax 94
Summary 95
Q&A 95
Workshop 96
Quiz 96
Quiz Answers 96
Activities 96
HOUR 7: Declaring a Class in an Interface File 97
Letting Xcode Do the Work 97
Designing Classes 97
Getting Ready to Create the Class 98
Exploring Class Hierarchies 104
Calling Superclass Methods 104
Introducing Protocols 106
Declaring Classes 106
Writing a Basic Class Declaration 106
Using Forward References 107
Summary 109
Q&A 109
Workshop 109
Quiz 109
Quiz Answers 109
Activities 109
HOUR 8: Declaring Instance Variables in an Interface File 111
Declaring Instance Variables and Properties 111
Using the Class 111
Placing a Class Instance in Context 112
Choosing the Context 113
Creating an Instance Variable for CurrencyConverter with id 114
What Happens When Execution Stops 115
Dynamic Binding 117
Creating an Instance Variable for CurrencyConverter with the Class Name 117
Creating an Instance Variable for CurrencyConverter with a Superclass Name 119
Managing Instance Variable Visibility 123
Summary 124
Q&A 124
Workshop 124
Quiz 124
Quiz Answers 124
Activities 125
HOUR 9: Declaring Properties in an Interface File 127
Comparing Interface Variables and Properties 127
Reviewing Basic Variable Declarations 128
Creating Declared Properties: The Basics 130
Dealing with Memory for Objects 132
Working with Attributes for Declared Properties 133
Using Declared Properties 133
Accessing the Property with Message Syntax 134
Accessing the Property with Dot Syntax 134
Using Attributes 135
Accessor Methods 136
Writability 136
Setter Semantics 137
Atomicity 137
Using Other Attribute Decorators 137
Implementing Properties 138
Creating Accessors with @synthesize 138
Promising Data with @dynamic 139
Summary 139
Q&A 139
Workshop 139
Quiz 139
Quiz Answers 139
Activities 140
HOUR 10: Declaring Methods in an Interface File 141
Working with Methods in a Class 141
Reviewing Method Syntax 142
Distinguishing Between Class and Instance Methods 142
Exploring the Method Declaration 143
Writing the Method Declaration 146
Returning Complex Data Structures from Methods 146
Summary 147
Q&A 147
Workshop 148
Quiz 148
Quiz Answers 148
Activities 148
HOUR 11: Declaring Actions in an Interface File 149
Introducing Actions 149
What Actions Can Do for You 150
Comparing Actions in OS X and iOS 159
Disconnecting Actions 161
Summary 162
Q&A 162
Workshop 163
Quiz 163
Quiz Answers 163
Activities 163
HOUR 12: Routing Messages with Selectors 165
Getting Inside Objective-C Messages 165
Receiver and Selector Objects in Messages 166
Getting Inside the Objective-C Runtime 167
Working with SEL and @selector () 168
Using performSelector 169
Creating a Selector with @selector () 169
Creating a Selector from a String 169
Using a Selector 170
Using NSInvocation 172
Creating an NSInvocation 172
Using NSInvocation Properties 173
Invoking an NSInvocation 175
Testing Whether an Instance Can Respond to a Selector 175
Summary 176
Q&A 176
Workshop 177
Quiz 177
Quiz Answers 177
Activities 177
HOUR 13: Building on the Foundation 179
Exploring the Foundation Framework 179
Foundation Classes 180
Root Classes 180
Other Classes 181
Foundation Paradigms and Policies 182
Mutability 182
Class Clusters 183
Notifications 184
Summary 187
Q&A 187
Workshop 187
Quiz 187
Quiz Answers 187
Activities 187
HOUR 14: Defining a Class in an Implementation File 189
Working with a New Project 189
Reconsidering Dynamic Typing 190
Designing the (Currency) Converter 190
Creating a New App 193
Implementing a Method 197
Expanding the Class with init Methods 200
Summary 202
Q&A 202
Workshop 202
Quiz 202
Quiz Answers 202
Activities 203
HOUR 15: Organizing Data with Collections 205
Collecting Objects 205
Getting Familiar with Property Lists 207
Using Collections in Property Lists 208
Building Collections from Property Lists at Runtime 209
Comparing the Collection Classes 209
Creating a Collection 210
Using the Common Collection Creation Methods 211
Using Objective-C Literal Syntax 212
Reading and Writing Arrays 213
Reading and Writing Dictionaries 213
Creating Sets 214
Enumerating a Collection 214
Examining NSEnumerator Methods 214
Creating NSEnumerator Instances for Collections 215
Testing Membership in a Collection 217
Accessing an Object in a Collection 218
Summary 218
Q&A 218
Workshop 219
Quiz 219
Quiz Answers 219
Activities 219
HOUR 16: Managing Memory and Runtime Objects 221
Managing Objects in Memory 221
Managing Reference Counts Manually 222
Looking at Memory Management Before ARC 223
Summarizing Memory Management 225
Managing Reference Counts with ARC 225
Using Declared Property Attributes 226
Variable Qualifiers 227
Autoreleasing Variables 228
Summary 229
Q&A 229
Workshop 229
Quiz 229
Quiz Answers 229
Activities 229
PART III: EXPANDING AND EXTENDING CLASSES
HOUR 17: Extending a Class with Protocols and Delegates 231
Exploring the Pros and Cons of Subclassing 231
Introducing the Example 232
Working with Protocols 232
Working with Delegates 233
Putting Protocols and Delegates Together 233
Looking Deeper Inside Protocols 236
Summary 236
Q&A 236
Workshop 237
Quiz 237
Quiz Answers 237
Activities 237
HOUR 18: Extending a Class with Categories and Extensions 239
Comparing Categories and Protocols 239
Choosing When to Use a Category 240
Comparing Other Techniques with Categories 240
Comparing Categories with Subclasses 241
Modifying a Class Hierarchy 241
Confining Modifications to Categories 242
Working with Categories 242
Using Class Extensions 245
Working with Informal Protocols 246
Summary 246
Q&A 246
Workshop 247
Quiz 247
Quiz Answers 247
Activities 247
HOUR 19: Using Associative References and Fast Enumeration 249
Catching Up on Objective-C 2.0 Time-Saving Features 249
Extending Classes by Adding Instance Variables (Sort of) 250
Adding an Associative Reference 251
Getting and Setting an Associative Reference 252
Removing an Associative Reference for a Key 254
Removing All Associative References from an Object. 254
Using Fast Enumeration 254
Using Fast Enumeration 254
Using Fast Enumeration with an NSEnumerator 256
Summary 256
Q&A 256
Workshop 257
Quiz 257
Quiz Answers 257
Activities 257
HOUR 20: Working with Blocks 259
Revisiting Blocks 259
Looking at Callbacks 260
Considering Callback Routines 263
Introducing Blocks 264
Creating a Block as a Block Variable 264
Using a Block Variable 265
Exploring Blocks in Cocoa 266
NSString enumerateLinesUsingBlock 267
NSArray enumerateObjectsUsingBlock 268
NSSet enumerateObjectsUsingBlock 268
Looking Deeper into Cocoa Blocks and Memory 269
Summary 269
Q&A 269
Workshop 270
Quiz 270
Quiz Answers 270
Activities 270
PART IV: BEYOND THE BASICS
HOUR 21: Handling Exceptions and Errors 271
Rethinking Exceptions and Errors 271
Introducing the Exception and Error Classes 272
Using Exceptions 273
Using Errors 273
Looking Inside NSException 274
Looking Inside NSError 274
Identifying an Exception 277
Throwing an Exception 278
Catching an Exception 280
Summary 280
Q&A 280
Workshop 280
Quiz 280
Quiz Answers 281
Activities 281
HOUR 22: Grand Central Dispatch: Using Queues and Threading 283
Getting Started with Concurrency 283
Looking at Processors Inside Computers 283
Using Concurrency Without Rewriting User Apps 284
Using Threads for Concurrency 284
Introducing Grand Central Dispatch (GCD) 285
Introducing Queues 286
Dispatch Queues 287
Dispatch Sources 287
Operation Queues 287
Using Dispatch Queues 288
Using Global Concurrent Dispatch Queues 288
Adding a Task to a Global Concurrent Queue 289
Designing with Queues 290
Summary 290
Q&A 290
Workshop 291
Quiz 291
Quiz Answers 291
Activities 291
HOUR 23: Working with the Debugger 293
Logging Information 293
Using Console Logs 294
Using NSLog 295
Enhancing NSLog 295
Using Smart Breakpoints 297
Enhancing Breakpoints with Messages 298
Breaking on a Condition 300
Summary 302
Q&A 302
Workshop 303
Quiz 303
Quiz Answers 303
Activities 303
HOUR 24: Using Xcode Debug Gauges for Analysis 305
Putting Debug Gauges in Perspective 305
Monitoring CPU Utilization 306
Monitoring Memory Utilization 307
Monitoring Energy 308
Using Instruments 310
Summary 311
Q&A 311
Workshop 312
Quiz 312
Quiz Answers 312
Activities 312
PART V: APPENDIXES
Appendix A: C Syntax Summary 313
Data Types 313
Enumerated Type 313
Struct Type 314
Pointers 314
Arrays 315
Control Structures 315
if Statements 315
switch Statements 315
Repeat Statements 316
Appendix B: Apps, Packages, and Bundles 317
Looking Inside a Project Bundle 317
lproj Files 318
Asset Catalogs 318
plist Files 319
Precompiled Header Files (.pch) 319
Appendix C: Archiving and Packaging Apps for Development and Testing 321
Archiving 321
Index 325