Home > Store

Sams Teach Yourself Objective-C in 24 Hours, 2nd Edition

Register your product to gain access to bonus material or receive a coupon.

Sams Teach Yourself Objective-C in 24 Hours, 2nd Edition


  • Sorry, this book is no longer in print.
Not for Sale


  • Copyright 2014
  • Dimensions: 7" x 9-1/8"
  • Pages: 368
  • Edition: 2nd
  • Book
  • ISBN-10: 0-672-33449-6
  • ISBN-13: 978-0-672-33449-8

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

Sample Content

Online Sample Chapter

Using Object-Oriented Features in Objective-C

Sample Pages

Download the sample pages (includes Chapter 3 and Index)

Table of Contents

Introduction     1
Who Should Read This Book     1
What This Book Covers     1
Downloading the Sample Files     2
How This Book Is Organized     2

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

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

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

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

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


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020