Objectively Speaking: A Crash Course in Objective-C for iOS 6
Note from Ray: This tutorial is now fully up-to-date for iOS 6, and uses Modern-Objective-C syntax. Here’s the pre iOS 6 version if you need it!
Are you a software developer skilled in another platform, but want to start learning iPhone development (and hence Objective-C)? This was my situation not so long ago, and frankly, I’d gotten a bit rusty from moving further and further away from development in my day job.
Some two years later, I’ve created a lot of apps for the iPhone and iPad. While I often had to learn the hard way, I’d like you to be able to benefit from some of my trials and tribulations.
This tutorial is for readers who already have some programming experience. It assumes that you know your while loop from your fruit loops and a debug from a lady bug! If you’re completely new to programming, you might want to check out our iOS for High School Students series.
The goal of this tutorial is to give you confidence with some of the basics of Objective-C. Instead of taking a “let’s explain every piece of syntax” approach, you’re going to take the approach of diving right in and giving you some hands-on experience and an example to work from. It will then be a lot easier for you to consult a reference (like this one) when necessary moving forward.
In this tutorial, you will create a simple app that randomly generate quotes from a stored list. In doing so, you’ll become acquainted with a number of aspects of Objective-C, including:
- Property lists
- Random selection
- Simple interface objects and events
Let me warn you though – iPhone development with Objective-C is a lot of fun and a little addictive. Be prepared to give up some sleep, and the chores might build up! :]
Before you begin, make sure you have an Apple developer account with the provisioning details set up and the latest version of Xcode installed (you can download this for free on the Mac App Store).
When you’re done, I will (Objective) C-you after the jump! :]
First things first: create an Xcode project. This tutorial is written for Xcode 4.5+ and iOS 6+ – if you have an older version of Xcode, you should either upgrade or check out the pre iOS 6 version of this tutorial.
Start up Xcode and create a new project with the iOS\Application\Single View Application template.
Enter QuoteGen for the product name, set device family to iPhone, and make sure Use Automatic Reference Counting and Use Storyboards are checked (but leave the other checkboxes unchecked). Now click Next and select the location to save your project.
You will notice that your project has been created with AppDelegate.h, AppDelegate.m, ViewController.h and ViewController.m files, as well as a MainStoryboard.storyboard.
The AppDelegate contains the code that initiates the app. For this tutorial, that’s all you need to know. Here are brief explanations of the other files, with which you will be working directly:
- MainStoryboard.storyboard is the interface layout file. You visually create/design the screens that are displayed on the iPhone device using this file.
- ViewController.m is the interface controller class. The interface layout file is linked to this class. This happens automatically, so at this stage you don’t need to think about it other than to know that any objects or events you set up in your interface class can be linked to your interface layout very easily. This is the file that will contain the Objective-C code you’re about to create.
- ViewController.h is the interface controller class header file, where you’ll declare the instance variables, as well as the objects and events that you need to access from the interface screen.
Note: There are two ways to create interfaces in Xcode – with Storyboards and with Xcode Interface Builder files (XIBs). Either way works fine, in this tutorial we’ll be using Storyboards since it’s the most common way these days. But the two methods are quite similar – if you understand one you’ll have an easy time understanding the other.
For more information on Storyboards, check out this tutorial when you’re done.
Heading in the Right Direction
The first thing you need to do is create some variables for the quotes you want to display in this app – normal quotes and movie-related quotes.
To do this, you will create two Objective-C properties. There’s some subtle aspects to properties, but for now just think of them as a way to create variables on your class.
It’s very easy to create a property – let’s try it out by adding a property for an array of quotes. Add this line to ViewController.h, between the @interface and @end lines:
@property (nonatomic, strong) NSArray *myQuotes;
Let’s break this down bit-by-bit:
- First you need to add the @property keyword.
- Then you list the property attributes. Without going too deeply into this topic – the nonatomic attribute increases performance at the cost of thread-safety, and the strong attribute indicates that a pointer to the specified variable will stay in memory as long as the object that holds the pointer exists.
- Then you list the type of the property. Here you choose NSArray *, which means “pointer to an NSArray class.” NSArray is a handy class provided by Apple that stores lists of data – we’ll talk more about this soon.
- Finally, you add the property name.
By adding this single line, you now have a variable you can set and get on this class!
Note: In the old days, you also had to @synthesize your properties after creating them, and in the really old days you had to manually declare your instance variables as well. This is no longer the case – now all you need to do to add a property is add this single line!
Also in the old days, you used to have to do all the memory management yourself, but now with a new feature called Automatic Reference Counting (ARC) it’s automatic. To read more about ARC, check out this tutorial.
Am I showing my age by knowing all this? :]
This app is also going to store some famous quotes from movies. For this, you need a second array:
@property (nonatomic, strong) NSMutableArray *movieQuotes;
Here you’re using an NSMutableArray simply to illustrate the different types of arrays. The difference is you cannot add or remove items to an NSArray after you create it, but you can add or remove items from NSMutableArray whenever you want.