Objective-C Programming: The Big Nerd Ranch Guide
by Aaron Hillegass and Mikey Ward
Copyright 2013 Big Nerd Ranch, LLC.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, contact
Big Nerd Ranch, LLC.
1989 College Ave NE
Atlanta, GA 30317
(404) 478-9005
http://www.bignerdranch.com/
book-comments@bignerdranch.com
The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, LLC.
Exclusive worldwide distribution of the English edition of this book by
Pearson Technology Group
800 East 96th Street
Indianapolis, IN 46240 USA
http://www.informit.com
The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
App Store, Apple, Cocoa, Cocoa Touch, Instruments, Interface Builder, iMac, iOS, iPad, iPhone, iTunes, Mac, OS X, Objective-C, PowerBook, and Xcode are trademarks of Apple, Inc., registered in the U.S. and other countries.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
Print ISBN-10 032194206X
Print ISBN-13 978-0321942067
Second edition, November 2013
Release K.2.1.2
Acknowledgments
It is a great honor that we get to work with such amazing people. Several of them put a lot of time and energy into making this book great. Wed like to take this moment to thank them.
The other instructors who teach our Objective-C class fed us with a never-ending stream of suggestions and corrections. They are Scott Ritchie, Bolot Kerimbaev, Christian Keur, Jay Campbell, Juan Pablo Claude, Owen Mathews, Step Christopher, TJ Usiyan, and Alex Silverman.
Sarah Brown, Sowmya Hariharan, Nate Chandler, and James Majors kindly helped us find and fix flaws.
Our brilliant editor, Susan Loper, took a stream-of-consciousness monologue that stumbled across everything a programmer needs to know and honed it into an approachable primer.
Ellie Volckhausen designed the cover.
Chris Loper at IntelligentEnglish.com designed and produced the print book and the EPUB and Kindle versions.
The amazing team at Pearson Technology Group patiently guided us through the business end of book publishing.
Table of Contents
Part I
Getting Started
You and This Book
Lets talk about you for a minute. You want to write applications for iOS or OS X, but you have not done much (or any) programming in the past. Your friends have raved about other Big Nerd Ranch books (like iOS Programming: The Big Nerd Ranch Guide and Cocoa Programming for Mac OS X), but they are written for experienced programmers. What should you do?
Read this book.
There are similar books, but this one is the one you should read. Why? We have been teaching people how to write applications for iOS and the Mac for a long time now, and we have identified what you need to know at this point in your journey. We have worked hard to capture that knowledge and dispose of everything else. There is a lot of wisdom and very little fluff in this book.
Our approach is a little unusual. Instead of simply trying to get you to understand the syntax of Objective-C, we will show you how programming works and how experienced programmers think about it.
Because of this approach, we are going to cover some heavy ideas early in the book. You should not expect this to be an easy read. In addition, nearly every idea comes with a programming experiment. This combination of learning concepts and immediately putting them into action is the best way to learn programming.
C and Objective-C
When you run a program, a file is copied from the file system into memory (RAM), and the instructions in that file are executed by your computer. Those instructions are inscrutable to humans. So, humans write computer programs in a programming language. The very lowest-level programming language is called assembly code . In assembly code, you describe every step that the CPU (the computers brain) must take. This code is then transformed into machine code (the computers native tongue) by an assembler .
Assembly language is tediously long-winded and CPU-dependent (because the brain of your new iMac can be quite different from the brain of your well-loved, well-worn PowerBook). In other words, if you want to run the program on a different type of computer, you will need to rewrite the assembly code.
To make code that could be easily moved from one type of computer to another, we developed high-level languages. With high-level languages, instead of having to think about a particular CPU, you can express the instructions in a general way, and a program (called a compiler ) will transform that code into highly-optimized, CPU-specific machine code. One of these high-level languages is C. C programmers write code in the C language, and a C compiler converts the C code into machine code.
The C language was created in the early 1970s at AT&T. The Unix operating system, which is the basis for OS X and Linux, was written in C with a little bit of assembly code for very low-level operations. The Windows operating system is also mostly written in C.
The Objective-C programming language is based on C, but it adds support for object-oriented programming. Objective-C is the programming language that is used to write applications for Apples iOS and OS X operating systems.
How this book works
In this book, you will learn enough of the C and Objective-C programming languages to learn to develop applications for the Mac or for iOS devices.
Why are we going to teach you C first? Every effective Objective-C programmer needs a pretty deep understanding of C. Also, many ideas that look complicated in Objective-C have very simple roots in C. We will often introduce an idea using C and then push you toward mastery of the same idea in Objective-C.
This book was designed to be read in front of a Mac. You will read explanations of ideas and carry out hands-on experiments that will illustrate those ideas. These experiments are not optional. You will not really understand the book unless you do them. The best way to learn programming is to type in code, make typos, fix your typos, and become physically familiar with the patterns of the language. Just reading code and understanding the ideas in theory will not do much for you and your skills.
For even more practice, there are exercises called Challenges at the end of each chapter. These exercises provide additional practice and will make you more confident with what you have just learned. We strongly suggest you do as many of the Challenges as you can.
You will also see sections called For the More Curious at the end of some chapters. These are more in-depth explanations of topics covered in the chapter. They are not absolutely essential to get you where you are going, but we hope you will find them interesting and useful.