Welcome to my series on How To Make iPhone Apps with no programming experience – where we’ll turn even the non-programmer into an iOS developer! Here’s the article index for the entire series in case you missed it.
Whether you’re learning iOS development, Java or any programming language for that matter, there are core computer programming concepts that are central to learning how to program. That’s what we’re going to go through today.
In this programming primer, we’re going to start at the very beginning and assume that you are not a programmer and don’t have any knowledge of computer programming. The concepts introduced in this tutorial will help you absorb the subsequent Objective-C and iPhone programming tutorials with a better understand of what’s going on.
We’re going to introduce the following object oriented programming concepts which many programming languages use (including Objective C).
1. Programming is like…
It may be odd to think of it this way, but programming is much like designing something.
Think of an application as a stage play and you’re the playwright, otherwise known as the person who writes the play.
Let’s make it concrete; let’s say that you’re Shakespeare and you’re writing an app called Romeo & Juliet.
Before you can choose an actor and actress to play the roles, you have to define what the roles are first, right?
Well, when you’re building an app, you do this by creating classes.
We said above that classes are like descriptions of roles in a play.
It describes how something will behave and act as well as its characteristics.
In our Shakespeare metaphor, we might define two classes: a Romeo class and a Juliet class.
These two classes will describe the roles that we need in our play. Within each class, we’ll need to store bits of information about each role so we use something called variables.
A variable in programming is used to store or point to data.
With our Romeo and Juliet classes, we might define variables in each class such as hair color, eye color, height and so on. These variables in the Romeo class can then store the data for Romeo’s hair color, eye color and height. And similarly, we’ll define the same variables in the Juliet class to store Juliet’s hair color, eye color and height.
Next, our classes also need to describe the actions that these roles will take, similar to a script.
There are lines that Romeo needs to say and actions that Romeo needs to perform. Likewise for Juliet.
We define these instructions in each class by using methods.
A method in a class is like a set of instructions that can be called upon to perform those instructions. Each method has a name and the name simply has to be called upon and the instructions inside the method will be executed.
So in our Romeo and Juliet example, the tragic final scene of the play has Romeo believing that Juliet has passed and so he ends his own life.
So as unfortunate as it is, we could create a method called “endLife” in the Romeo class so that when this method is called, Romeo will take his own life 🙁
5. Class Inheritance
Class inheritance is a way for you to define a class that is based on another class.
The class that is being inherited from is known as the parent or super class and the new class that is doing the inheriting is called the child or subclass.
In our Shakespearean example, there are particular variables that both the Romeo class and the Juliet class share in common. Namely, hair color, eye color and height.
Instead of defining these same 3 variables in both classes, we can create a brand new class called “Person” and define these 3 variables in the Person class.
Then we specify that Romeo inherits from the Person class. In other words, Romeo is a subclass of Person. And Juliet is also a subclass of the Person class.
Now, both the Romeo and Juliet class will also have the 3 variables. The same can be done for methods.
If there’s a method that both classes should have, we can define it in the Person class instead. Say, for example, a “Kiss” method.
However, for the “endLife” method, only Romeo should have that method, so we define the “endLife” method in the Romeo class instead of the Person class.
Now that we have our roles defined, we can finally hire an actor to play Romeo and an actress to play Juliet.
Well in programming, once we have our classes defined, we can create an instance of that class called an object.
So we can create a Romeo object from the Romeo class and a Juliet object from the Juliet class. It’s these two objects that will interact with each other.
Just like an actor and actress will bring the roles to life, creating an object from the class is bringing that class to life and it will behave exactly like you’ve defined it. It will have the variables you gave it and the functionality that you defined using methods.
The only difference between creating objects and hiring actors/actresses is that with programming, you can create multiple objects from the same class. So you can create two Romeo objects from the class and you can set the hair color of Romeo object #1 to yellow and the hair color variable of Romeo object #2 to brown.
That will make for an interesting Romeo & Juliet play!
7. Another Example
Another way to think of defining a class is like defining a blueprint for a robot.
The blueprint describes the robot’s properties and how it behaves and acts with other robots.
For example, we might define the classes below for an application that fetches some data and displays it to the user on the screen.
During the execution of the application, instances (objects) of each class may be created and then their methods carried out to generate the desired result.
Remember, a variable is used to keep track of a piece of data.
When we define variables in classes, we are usually defining them to keep track of information that is vital to that class.
Because variables are defined as part of a class, all objects of the class will have that variable.
In the example above, we might define a variable in the DataBot class to hold on to the data that it has fetched. Let’s call that variable “robotPocket” (as if robots store data in their pockets!!)
If we create an DataBot object, it’ll have this variable “robotPocket” but it’ll be empty initially.
After our DataBot object fetches the data, we’ll store it in the “robotPocket” variable which will later be passed onto ControllerBot.
Similarly, ControllerBot will need a variable to hold the data that it gets from DataBot.
And ViewBot will need a variable to hold the data that it gets from ControllerBot.
Next, we have to define the methods in the class and fill each method with instructions that may involve retrieving values from variables, assigning values to variables, calling other methods of the same object or even calling methods of other objects.
In DataBot, we could define a method called “getData” which would contain the instructions for DataBot to get the data we need to display to the user.
8. What’s Next?
Hopefully these analogies haven’t made things even more confusing for you! As a beginner trying to learn to program, these programming concepts may be hard to grasp at first but once it clicks, it’s going to become second nature to think this way.
Now that you’ve got some notion of what classes, variables, methods and inheritance are you can proceed to the first article in the How To Make iPhone Apps Crash Course!
I want to make this series and this article in particular the best and easiest to understand resource for beginners to learn how to develop iPhone apps. If you’ve got any feedback, good or bad, please let me know via the comments below so I can update parts which are confusing, add more examples and to continue updating this resource.
And if you’ve enjoyed this article or you know someone who might be interested in learning how to build iOS apps, please share this series with them. It’s targeted at the non-programmer (sort of like an iPhone application development for dummies book) so anyone you know will be able to pick this up and progress towards developing iPhone apps!