Lesson 1: Introduction to Objective-C Programming

Objects, syntax and methods

--

The programming language that developers use to write iPhone applications is called Objective-C, an object-oriented language based upon the standard programming language C. Object-oriented languages are categorized as such because they incorporate the programmatic representation of objects, such as cars and people, in order to run. As C does not use objects, Objective-C is titled as such because it is merely a language built atop C that uses objects. Hence, Objective-C. Apple uses this language because it is efficient, easy to write and neat. Because it is based upon the C language, Objective-C can incorporate blocks of C code (as well as C++), making it very versatile for application development. For more on general object-oriented programming, click here.

Disclaimer: As stated in the course introduction, this collection assumes that you have a basic understanding of the C and/or C++ language. You don’t need to be a master, but you should be familiar with coding. If you don’t know the basics of C, check out this tutorial or this tutorial.

Object-Oriented Programming in Objective-C

If you are familiar with object-oriented programming, skip this section.

Objective-C programming is based upon the concept of code representing real-life objects. Representations of programmatic objects can range from strings (representation of text—a string of characters), to labels, to even people. In Objective-C, objects are members of their classes. A single object, or representation of a class, is called an instance of that class. Each instance of a class has properties, things that define and characterize it. Additionally, each instance has methods, or things it can do. This is all pretty confusing to a beginner, so I’ll explain these concepts with a real-world example—how about people.

You and I are each a person. We have millions of characteristics that define us, but we are both people. We have eyes, ears, personalities, tastes, et cetera. Additionally, we can all do things. We can express emotion, sing songs, laugh…you get the picture. So if we were to characterize ourselves programmatically, we could say that our class is Person. Our characteristics, such as eye color, hair color and favorite type of car, are properties. Finally, our abilities, such as talking, laughing and coding, are methods.

Programmers create classes in order to manage the relationship between what the computer is doing and what they want to accomplish in real life. A developer might create a PickupTruck class in order to represent a truck in his program. In the program, he could create an instance of PickupTruck, with properties such as color, production year and bed size. Additionally, he can make the truck do things, such as drive, refuel and slow down. The possibilities are endless.

Class Hierarchy and Inheritance

Object-oriented programming complicates with the ideas of class hierarchy and inheritance. Class hierarchy is the notion that a class can derive its properties and methods from its parent, or superclass, while being able to implement new properties and methods. Inheritance is just another term for class hierarchy—a daughter class inherits properties and methods from its parent class. To explain further, I’ll append my pickup truck example.

Let’s start with the PickupTruck class. It has basic traits (capacity, production year and bed size) and abilities (drive, refuel and slow down). Just as calling a vehicle a pickup truck in real life is a generalization, an instance of PickupTruck in our example is pretty general, too.

Now let’s add a daughter class—FordPickupTruck. It will inherit all the properties of PickupTruck, as it has a color, production year and bed size. Additionally, it inherits the methods of PickupTruck—it can drive, refuel and slow down. But since FordPickupTruck is a more specific representation, it has more properties and methods. An instance of FordPickupTruck will have a model name—a trait too specific for a general PickupTruck. We can also add an ability, let’s say an instance of FordPickupTruck can tow, something not all PickupTrucks can do.

While FordPickupTruck inherits properties and methods from its superclass, PickupTruck, it also has expanded traits and abilities not present in all types of PickupTruck.

A parent class can have an unlimited amount of daughter classes, which in turn can all have an unlimited amount of daughter classes and so on. A class’ inheritance pattern can be linear, or it can branch out infinitely.

Linear inheritance
Branching inheritance

Programmatically, class hierarchy and inheritance may seem confusing. However, if you give them real-world examples, they will become much simpler, and thus you will be more able to understand them. This holds true for many difficult concepts in computer science.

Syntax

Objective-C uses the same phraseology as the C language. Like in C, each line of Objective-C code must end with a semicolon. Blocks of code are written within a set of curly brackets. All basic types are the same, such as int, float, double, long and more. Additionally, all the mathematical symbols are the same, as well as comparison and assignment symbols, as displayed in the table below.

All mathematical, assignment and comparison symbols are the same in C, C++ and Objective-C.

C and Objective-C share many similarities. However, one they do not share is syntax. Method calling, enumeration, function composition and many more properties of Objective-C differ greatly. Here are some very distinctive differences.

Some fundamental differences between C/C++ and Objective-C.

For more on the differences between C/C++ and Objective-C, check out this very helpful guide. It’s a bit lengthy, but it covers pretty much everything.

A fundamental difference between Objective-C and C/C++ is that while the latter can be written anywhere in a file, Objective-C code MUST be written within the interface (header) or implementation of a class. Objective-C methods cannot be called alone, they must be called by an object. If this confuses you, ignore it. We’ll revisit it in the next lesson.

Method Composition and Calling

The best way to learn a new language is to dive right in. Accordingly, let’s start by analyzing a simple function in Objective-C, shown below.

A simple halving function in Objective-C.

While the code within the brackets is indistinguishably Objective-C or C/C++, the function header (the code before the first bracket) is distinctly Objective-C. It commences with a minus sign, followed by the parenthesized return type. After that is the method name, followed by a colon and parameter. Unlike in C/C++, the parameter’s type is parenthesized, while the name of the parameter is not. The same function in C/C++ would be written as shown below.

The halving function, written in C/C++.

Truthfully, while the difference between function composition is distinct, it is not difficult to learn. Just remember this basic formula for writing methods in Objective-C:

Regarding method composition, know this formula and you’re good to go.

Calling methods in Objective-C is not only much simpler than composing them, but also much simpler than in C and C++. While in C++, there are two ways to invoke methods (pointers and dot notation), in Objectice-C, there is only one: using square brackets. No matter the type of method you’re invoking on an object, you will use brackets to do so. Let’s call that method we wrote earlier.

Notice any similarities between function composition and calling?

Here, we declare the variable quotient as a float and assign it the return value of our divideNumberByTwo: method. In this case, since the number parameter is 200, the value of quotient would be 100.

Nested Methods

A property of Objectice-C method calling that can quickly become an eyesore is nested methods, or methods inside methods. If you think multiple parentheses are a pain, wait until you have a method with 10 square brackets…

A nested method

While multiple nests do occur, they are avoidable by expanding them into multiple lines of code:

The previous method, expanded into 4 lines of code

Conclusion

You should now be familiar with the concepts listed below.

  • Object-oriented programming
  • Classes and instances
  • Inheritance and Class Hierarchy
  • Objective-C syntax
  • Differences between C/C++ and Objective-C
  • Method composition and invoking
  • Nested methods

In the next lesson, we’ll take a look at creating our own classes, as well as writing our first bit of code in Objective-C. Thanks for reading, and code on!

Copyright 2013 William Harrison Development. All Rights Reserved.

--

--