Learn Objective-C and Start Building your First App Today

learnobjectivecThe only way to create software for Macs, Objective-C is an object-oriented language allowing Apple developers to design apps for the iPhone and iPad. If you have a programming background and are familiar with the C language, this introduction is going to make a lot of sense. If you are just starting out, you may have more difficulty. For the newbies, don’t stress – you can go from a beginner to a skilled C language programmer in just a few hours.

Objective C is a great language to learn. A beginner who understands the basics of the C language can progress rapidly in their iOS development. This introduction will detail the main components in Objective-C, and finish with a small program to run. As always, the official Apple Objective-C guide is a worthy resource for any serious developer,

An ability to master the details is a key trait of every good developer. Paying attention to the specific prompts and commands in your code is the most difficult. Objective C isn’t hard, many programming languages share similar traits. In actuality, the programming language doesn’t even matter. Ultimately, you’re building a set of commands to control a computer, whether it is for your hot new app or to run a program – every programming concept is the same. Loops, conditionals, variables, all you need to do is learn the words and commands to control your machine in your language of choice. If you are getting lost with the terminology, I recommend mastering the fundamentals of programming (for non programmers!), you will be speaking like a pro in no time!

Background

Originally developed in the 1908’s, Objective C is derived from the C programming language. It gained rapid popularity with the rise of the iPhone, as it’s the language you need to build apps on Mac’s iOS. Many other programming languages have come from C. C++ is popular, as it adds classes and other object-oriented features. Microsoft expanded on this to create C# (C Sharp), which was initially very similar to Java, but is now even further down the object-oriented road. The most fantastic part of all the C languages, is the core syntax is the same. Once you understand variables, how they are declared and used is often the same, and keywords and symbols are usually similar.

Makes it simple right?

Understand “Object-Oriented”

To better understand what I mean by “object-oriented” programming language, let’s take an example from my daily life. Not surprisingly, an object based language is based on…objects! Look around your room, every piece of furniture, the separate items, all of these are classified in Objective-C as objects. The empty cereal bowl on my desk is an object. It’s main property is how much cereal it holds. Properties are great, allowing you to interact with an object through queries. I can query, is my cereal bowl empty?

If its still unclear, another way to consider object-orientation is like this. An object is a simple program that you can send a message to, to perform a simple action.

This is a great way to manipulate many physical objects using an app. In your iPhone, the camera object is interacted with as you take a photo. I could also create an application that turns on the camera’s flash object at the press of a button, building a simple flashlight app. Because we define all physical items as objects, the properties and queries we create can control these – entirely using code. If you are interested to learn more about building apps, Mark Lassoff has some great tutorials that will be essential for a beginner to master the skills needed in creating their first iOS app.

What makes up Objective-C

Programming is the set of instructions you develop to have your computer to perform certain tasks. At it’s bare essentials, its basically just about interacting with data. The level of complexity in the data is all that changes, it could simply be yes/no criteria, or detailed player information in a game.

Variables

Variables are very important, they are the storing place for data inside the Objective-C language. Able to hold any type of data, you must first specify the type of data you wish to store. In programming, you can store anything inside a variable. Your computer will assign a block of memory to hold this data, which you can then reference back to in your code using the unique name you labelled it with.

It’s very strict. If you assign numbers to a variable, you cannot put symbols or letters inside this “container.” Your program will check this when it runs, and if this is not set up correctly (you have forgotten to specify a data type, or you have used the wrong data type), an error will occur. Some of the most commonly used variables are:

  • int to store integers, numbers with no decimal point

  • char to store a specific character

  • float to store numbers with decimal points

  • BOOL to store a true/false or 0/1 value,

Objects

We touched on the ability for a programmer to send queries to objects. In Objective-C, all objects start with a capital letter. Examples of the most common:

  • NSString a string of characters together to from text

  • NSNumber a numerical value

You can’t query variables, which is why asking “length” to int would not get a result. The same query to a NSString object, will return the number of characters in the object.

Classes

The blueprint for defining objects, classes are a fast way to reuse certain behaviours and properties in Objective-C. Every object is defined by its class, which describes how an object should behave. All classes used in Objective-C are stored in the same place, which is why there is a naming convention. Classes always add 2-3 letters in front of a class name, to show where it has originated.

The “NS” is a nod to NeXTSTEP Computer, the company Steve Jobs founded in the 1980’s.

Conditionals

For an application to make a choice, it needs to use a conditional. Without this, you would see the exact same results every time you ran your application. Think of this like the IF function in Microsoft Excel. It checks to see if something is true, then acts either way. If you have never used Microsoft Excel, you can get a great headstart on your peers with just a couple of hours learning.

Loops

As simple as it sounds, loops let us execute a piece of code multiple times. Extremely handy for users wanting to re-populate a database, or perform a certain calculation until a conditional returns true. The most common are for, while and do.

Categories

Without having to access source code in the classes, categories allow you to repair or add functionality to a specific class. Adding a category can add a new behaviour to a specific class. If you then add an object of this class & category, the additional functionality will be available to the object. This is a great benefit that can allow you to generate minor add-ons to classes without having to change the source code (sometimes its not available, such as framework classes like NSString)

Putting it altogether

Lets jump into the code! First, let’s create a basic variable, and put some data into it. I’m going to use:

int favoriteNumber = 12;

This is quite simple, the code has done the following:

  • Create a variable named “favoriteNumber”

  • Assign this variable to contain integers “int” (whole numbers)

  • Using the equals “=” sign, we allocate the number 12

Essentially, we now have a container called “favoriteNumber” that holds the number 12. If we want to use or retrieve this data at a later point, we can get it using the variables name.

In programming, structure is very important. These are the guidelines you must follow in creating variables:

  1. Specify the data type (i.e. what kind of information the variable will hold)

  2. Name the variable. Don’t use any special characters or spaces, and they cannot start with a number. Otherwise, you can name it anything you want.

  3. Use an operator. In this example we use “=” to assign the value 12 to the variable.

  4. The value we are assigning is “12”. Because its an int number, it has to be either a positive or negative whole number (no decimal points!).

  5. Finish the statement with a semicolon. in Objective-C this acts like the period on a sentence, and indicates we have finished with this statement. Every statement in Objective-C must end with a semicolon, however sometimes complex statements are split over a number of lines to make them easier to read.

Object variables make things a little more complicated. Let’s use a different example.

NSString *title = @“Objective-C Basics”;

In this example, we have the following:

  • Data type is text (NSString is a text string).

  • The variable’s name is “title”

  • The operator “=” assigning the text value of “Objective-C Basics”.

Two points of note from this example:

  • But what about the asterisk? This has a special characteristic, its a pointer. More specifically, we are using the asterisk to de-reference a pointer. This is a great shortcut, a pointer is simply an address to the real location where the variable is holding the data. Pointers are a big part of the C language, they make your code much more efficient. Using pointers, you only need to store and copy a single pointer, instead of copying the data being pointed to each time. If you were using video in your code, a large video file will soon overload your program if you use all of its data each time, instead a pointer lets you retrieve the value stored in the computers memory from where it is pointing to.

  • The string is finished with the at symbol “@” and text inside double quotation marks. This indicates that the attached variable (or object) is special to Objective-C. This is important. When the computer starts using Objective-C code, NSString objects are different in C language than they are in Objective-C. The @ symbol is there to differentiate between the two.

Real life example:

For programmers that want to try out these tools, the simplest way is to first to focus on the language, and make some basic programs. If you haven’t already, install Apple’s developer tools, its free. If you are using Windows, you can build Objective-C programming on Cgywin or even MinGW.

Compiling your code is the first step, we will use the following example:

1. #include <stdio.h>

2. int main(){

3. printf(“Hello World\n”);

4. return 0;

5. }

My plan is for this application to run and display the string “Hello World” in Terminal. To test this code for yourself, open Xcode and make a new Objective-C class. Delete everything that is in there by default, and past the above code in and save the file as program1. Open Terminal, and find the location of your file (i.e. if it’s saved on your desktop type “cd desktop”) and then type:

gcc program1.m -o program1

Your program should compile with no errors, lets run it by typing ./program1 and hitting enter. Success!

This program is doing a number of things in sequence, lets look at what happened:

  • It imported a library called studio (this manages input/output functions like print)

  • We create a function called main that returns an integer value

  • We use print to output “Hello world” in Terminal

  • The \n tells Terminal to put a new line after the text

  • We return the integer of 0 which tell the operating system that everything went fine

To master more sophisticated app’s, I recommend following step by step with the professionals as they walk you through the development process. App Ninja have a fun course to create your own zombie pet, whilst Sidney Maestre has taken a more professional approach and will guide you through building your own version of AirBnB.

The possibilities in Objective-C are endless, learn from the best and start developing your dream app today!