Updated Oct 3 2013: This tutorial is compatible with XCode 5 AND XCode 4!
We’re taking a practical approach to learning iOS programming. This means lots of hands on work and practice!
In order to benefit the most from these lessons, you should open XCode and try to follow along.
This is part 3 of the lesson plan. Click here for the entire Learning Plan.
At a glance, your the new blank XCode project you created from the last tutorial has the following files:
Main.Storyboard (may not be named the same in XCode 4)
Notice how you have an AppDelegate.h as well as another one with a “.m” extension.
The .h is called the Header file and the .m is called the Implementation file.
Together, they form the AppDelegate class.
Wikipedia describes a class as:
In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects.
You can think of a class as a blueprint for something. Just like a blueprint for a building.
Using that single blueprint, you can create multiple instances of that building.
Similar to that, When we program iOS apps, we create and define classes to represent different entities and then we use those classes to create instances of that entity. We call these instances objects.
Now you can visualize the files in your XCode project like this:
Your default XCode project creates two classes for you; One called AppDelegate and another one called ViewController.
So what is Main.storyboard?
It turns out that the ViewController class is exactly what it sounds like. It’s a class that is responsible for controlling and managing a View!
So Main.storyboard is naturally the view that the ViewController class is managing!
To be more specific, the Storyboard describes the views and app flow.
If you click on Main.storyboard, you’ll notice that the main editor area of XCode changes into a visual designer for that view. This is known as “Interface Builder” and you will be using it very soon so contain your excitement for a moment and let’s continue going through these files!
Now that you understand that you have two classes and a view, let’s see how they all relate to each other.
Your Default iPhone App Flow
Here’s a simplified visual diagram of what’s happening in your default XCode project.
Looking at the diagram above, notice that the AppDelegate object is the entry point to your application.
Then the AppDelegate is responsible for creating a ViewController object when it notices that your app has launched.
The ViewController object is responsible for setting up the view described in Main.storyboard and showing it on the screen to the user.
And that simple app flow is how you get this!
Where Do Objects Live?
When you write code to create an object, it gets created in memory.
Think of memory as an abstract space to store and keep track of all of your objects.
The memory space is not persistent; meaning that if your app is stopped, then everything in memory is flushed and you’ll lose all of that data.
Furthermore, your app on the iPhone or iPad is only allotted a certain amount of memory.
So that means if you continue to create objects without removing the objects that you don’t need anymore, you’ll eventually run out of space in memory and your iPhone app may crash.
A long time ago, programmers would need to manage this on their own and release objects from memory when it was no longer needed but these days you can use Automatic Reference Counting which will automatically remove unused objects from memory for you! (Remember when we created our XCode project and we had “Use Automatic Reference Counting” selected?)
That’s a good place to end for this tutorial. If you’re new to programming, this concept of classes and instances of classes being objects is enough to wrap your head around but it’s a very critical part to understand!
If I can elaborate on it more to help you understand, please don’t hesitate to comment below and I’ll update the FAQ section on this page to benefit everyone.
Frequently Asked Questions
There are none right now but if you have a question related to this tutorial, please ask below and I’ll update this section!