iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) (3 page)

Read iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) Online

Authors: Aaron Hillegass,Joe Conway

Tags: #COM051370, #Big Nerd Ranch Guides, #iPhone / iPad Programming

BOOK: iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides)
10.21Mb size Format: txt, pdf, ePub
Bronze Challenge: Finding the Subforum
Silver Challenge: Swapping the Master Button
Silver Challenge: Processing the Reply
Gold Challenge: Showing Threads
Blocks and Block Syntax
Declaring block variables
Defining block literals
Executing blocks
More notes about blocks
Basics of Using Blocks
Variable Capturing
Typical Block Usage
For the More Curious: The __block Modifier, Abbreviated Syntax, and Memory
For the More Curious: Pros and Cons of Callback Options
The Need for Stores
Creating BNRFeedStore
Using the Store
Building BNRFeedStore
Initiating the connection
Another request
JSON Serialization
More on Store Objects
Bronze Challenge: UI for Song Count
Mega-Gold Challenge: Another Web Service
For the More Curious: JSON Data
Caching the RSS Feed
Advanced Caching
NSCopying
Finishing the BNR feed
Read and Unread Items
Other Benefits of Store Objects
Bronze Challenge: Pruning the Cache
Silver Challenge: Favorites
Gold Challenge: JSON Caching
For the More Curious: Designing a Store Object
Determining external sources
Determining singleton status
Determining how to deliver results
For the More Curious: Automatic Caching and Cache.db
iCloud Requirements
Ubiquity Containers
Provisioning a Ubiquity Container
Core Data and iCloud
For the More Curious: iCloud Backups
What to do next
Shameless plugs
Introduction

An aspiring iOS developer faces three basic hurdles:

 
  • You must learn the Objective-C language.
    Objective-C is a small and simple extension to the C language. After the first four chapters of this book, you will have a working knowledge of Objective-C.
 
  • You must master the big ideas.
    These include things like memory management techniques, delegation, archiving, and the proper use of view controllers. The big ideas take a few days to understand. When you reach the halfway point of this book, you will understand these big ideas.
 
  • You must master the frameworks.
    The eventual goal is to know how to use every method of every class in every framework in iOS. This is a project for a lifetime: there are over 3000 methods and more than 200 classes available in iOS. To make things even worse, Apple adds new classes and new methods with every release of iOS. In this book, you will be introduced to each of the subsystems that make up the iOS SDK, but we will not study each one deeply. Instead, our goal is get you to the point where you can search and understand Apple’s reference documentation.
 

We have used this material many times at our iOS Development Bootcamp at Big Nerd Ranch. It is well-tested and has helped hundreds of people become iOS application developers. We sincerely hope that it proves useful to you.

 
Prerequisites

This book assumes that you are already motivated to learn to write iOS apps. We won’t spend any time convincing you that the iPhone, the iPad, and the iPod touch are compelling pieces of technology.

 

We also assume that you know the C programming language and something about object-oriented programming. If this is not true, you should probably start with an introductory book on C and Objective-C, such as
Objective-C Programming: The Big Nerd Ranch Guide
.

 
What’s Changed in the Third Edition?

This edition assumes that the reader is using
Xcode 4.3
and running applications on an iOS 5 device or simulator.

 

With iOS 5, automatic reference counting (ARC) is the default memory management for iOS. We’ve redone the memory management chapter to address ARC, and we use ARC throughout the book.

 

You’ll find new chapters on using gesture recognizers, storyboards,
NSRegularExpression
, and iCloud. We’ve also added two chapters dedicated to the the Model-View-Controller-Store design pattern, which we use at Big Nerd Ranch and believe is well-suited for many iOS applications.

 

Besides these obvious changes, we made thousands of tiny improvements that were inspired by questions from our readers and our students. Every page of this book is just a little better than the corresponding page from the second edition.

 
Our Teaching Philosophy

This book will teach you the essential concepts of iOS programming. At the same time, you’ll type in a lot of code and build a bunch of applications. By the end of the book, you’ll have knowledge
and
experience. However, all the knowledge shouldn’t (and, in this book, won’t) come first. That’s sort of the traditional way we’ve all come to know and hate. Instead, we take a learn-while-doing approach. Development concepts and actual coding go together.

 

Here’s what we’ve learned over the years of teaching iOS programming:

 
  • We’ve learned what ideas people must have to get started programming, and we focus on that subset.
 
  • We’ve learned that people learn best when these concepts are introduced
    as they are needed
    .
 
  • We’ve learned that programming knowledge and experience grow best when they grow together.
 
  • We’ve learned that

    going through the motions

    is much more important than it sounds. Many times we’ll ask you to start typing in code before you understand it. We get that you may feel like a trained monkey typing in a bunch of code that you don’t fully grasp. But the best way to learn coding is to find and fix your typos. Far from being a drag, this basic debugging is where you really learn the ins and outs of the code. That’s why we encourage you to type in the code yourself. You could just download it, but copying and pasting is not programming. We want better for you and your skills.
 

What does this mean for you, the reader? To learn this way takes some trust. And we appreciate yours. It also takes patience. As we lead you through these chapters, we will try to keep you comfortable and tell you what’s happening. However, there will be times when you’ll have to take our word for it. (If you think this will bug you, keep reading – we’ve got some ideas that might help.) Don’t get discouraged if you run across a concept that you don’t understand right away. Remember that we’re intentionally
not
providing all the knowledge you will ever need all at once. If a concept seems unclear, we will likely discuss it in more detail later when it becomes necessary. And some things that aren’t clear at the beginning will suddenly make sense when you implement them the first (or the twelfth) time.

 

People learn differently. It’s possible that you will love how we hand out concepts on an as-needed basis. It’s also possible that you’ll find it frustrating. In case of the latter, here are some options:

 
  • Take a deep breath and wait it out. We’ll get there, and so will you.
 
  • Check the index. We’ll let it slide if you look ahead and read through a more advanced discussion that occurs later in the book.
 
  • Check the online Apple documentation. This is an essential developer tool, and you’ll want plenty of practice using it. Consult it early and often.
 
  • If it’s Objective-C or object-oriented programming concepts that are giving you a hard time (or if you think they will), you might consider backing up and reading our
    Objective-C Programming: The Big Nerd Ranch Guide
    .
 
How To Use This Book

This book is based on the class we teach at Big Nerd Ranch. As such, it was designed to be consumed in a certain manner.

 

Set yourself a reasonable goal, like

I will do one chapter every day.

When you sit down to attack a chapter, find a quiet place where you won’t be interrupted for at least an hour. Shut down your email, your Twitter client, and your chat program. This is not a time for multi-tasking; you will need to concentrate.

 

Do the actual programming. You can read through a chapter first, if you’d like. But the real learning comes when you sit down and code as you go. You will not really understand the idea until you have written a program that uses it and, perhaps more importantly, debugged that program.

 

A couple of the exercises require supporting files. For example, in the first chapter you will need an icon for your Quiz application, and we have one for you. You can download the resources and solutions to the exercises from
http://www.bignerdranch.com/solutions/iOSProgramming3ed.zip
.

 

There are two types of learning. When you learn about the Civil War, you are simply adding details to a scaffolding of ideas that you already understand. This is what we will call

Easy Learning

. Yes, learning about the Civil War can take a long time, but you are seldom flummoxed by it. Learning iOS programming, on the other hand, is

Hard Learning,

and you may find yourself quite baffled at times, especially in the first few days. In writing this book, we have tried to create an experience that will ease you over the bumps in the learning curve. Here are two things you can do to make the journey easier:

 
  • Find someone who already knows how to write iOS applications and will answer your questions. In particular, getting your application onto the device the first time is usually very frustrating if you are doing it without the help of an experienced developer.
 
  • Get enough sleep. Sleepy people don’t remember what they have learned.
 
Using an eBook

If you’re reading this book on a Kindle, KindleFire, or Kindle for iPad, we want to point out that reading the code may be tricky at times. Longer lines of code will wrap to a second line based on the current font size. This bothers us because we’re really conscientious at Big Nerd Ranch about the way our code appears on the page. Clear visual patterns in code make code easier to understand.

 

When you get to the point where you’re actually typing in code, we suggest opening the book on your Mac in Kindle for Mac, a free application you can download from Amazon.com. Make the application window large enough that you can see the code with no wrapping lines. You will also be able to see the figures in full detail.

 

The longest lines of code in this book are 86 monospace characters. Use the following sample to see how your Kindle application or device displays them.

 
[yummyIceCream addToppingsWithArray:[NSArray arrayWithObjects:caramel, vanilla, nil]];

You can play with the settings in your Kindle application or device to find the best ones for viewing long code lines. Here are some suggestions for reading on the various devices:

 
  • On Kindle for iPad, code lines that wrap are hyphenated. This is dangerous. If you type these extraneous hyphens in, they will definitely break your code.
 

To see the code with no wrapping, hold the device in landscape mode, touch the
Aa
icon, select either of the two smallest fonts in the top section, and select the single-column icon in the bottom section.

 
  • On the KindleFire or Kindle DX, you can see the code with no wrapping if you hold the device in landscape mode and select the smallest font size.
 
  • On the Kindle and Kindle Touch devices it is not possible to see the code without wrapping.

On the Kindle E-Ink devices, there is a bug in how strikethrough is rendered. (We use strikethrough to indicate that a line of code should be deleted.) The first word of a line may not get struck through. So be extra careful if you are typing in the code while reading the book on an E-Ink device. Again, we suggest using Kindle for Mac when typing in code.

 

Other books

Waiting for Godot by Samuel Beckett
Our Love by Binkley, Sheena
Tower of Terror by Don Pendleton, Stivers, Dick
When Venus Fell by Deborah Smith
Can't Let Go by A. P. Jensen
Waiting for Love by Marie Force