When I teach iPhone and iPad courses for Learning Tree, there are usually a few comments about Apple’s choice of Objective-C as the programming language for iOS. Typically something along the lines of, “Why couldn’t they just use XYZ?”.
It is true that Objective-C can appear strange at first. Most students that attend Learning Tree programming courses tend to have experience with the brace family of object-oriented languages: C++, C# and Java. These languages owe much of their design to C’s curly brace style and Simula’s approach to objects. Indeed, the creator of C++, Bjarne Stroustrup, specifically quotes C and Simula67 as his main influences when designing C++1. Objective-C, although it is a superset of C, borrows a lot of its object-oriented syntax and concepts from a sibling of Simula called Smalltalk.
Consequently, for many programmers introduced to iOS development, getting to grips with ObjectiveC is like getting to know a long lost cousin!
Perhaps the most alien feature of Objective-C is one that is most commonly used: calling a method on an object. C++, C# and Java all use the parenthesized parameter approach for method declarations. For example, this is how I would write a method signature on a Java class that calculates a gross amount from a net amount using a tax rate:
public double calculateGross(double net, double taxRate);
Objective-C, on the other hand, blends method arguments into the method declaration:
- (double)calculateGrossForNet:(double)net usingTaxRate:(double)taxRate;
What is important to appreciate here is that this is is a method with a signature of
calculateGrossForNet:usingTaxRate that takes two arguments, not a method called
calculateGrossForNet. Objective-C calls this a selector.
Now look what happens when we call the methods in Java and Objective-C:
// Java double gross = this.calculateGross(123.45, 8.5); // Objective-C double gross = [self calculateGrossForNet:123.45 usingTaxRate:8.5];
Notice how the Objective-C method call is self-documenting. It is completely clear what the two arguments are, whereas in Java (and C++ and C# take a similar approach), an educated guess or reference to the source code or documentation is required.
Method signatures and method calls are probably the most obvious difference between Objective-C and C++, C# and Java, but there are lots of similarities too. Java and C# implement multiple inheritance using interfaces. C++ has pure virtual classes. Objective-C has protocols. A nice touch in Objective-C is the ability to declare some protocol methods as required and some as optional.
Java programmers will perhaps enjoy more new language features than C# programmers when moving to Objective-C. Objective-C has categories, which are very similar to C# extensions. It also adopts a similar mechanism for synthesizing accessors and mutators to C#. The former is not available in Java and the latter is usually achieved through code-generation by the IDE.
Being a lightweight, mobile operating system, iOS does not have a garbage collection thread for Objective-C, so programmers have to manage memory manually. This is the norm in C and C++ but it can be a major headache for C# and Java programmers who are used to the allocate-and-forget approach. Fortunately, with the introduction of Automatic Reference Counting (ARC) in the LLVM compiler that comes with the iOS 5 developer kit, Java and C# programmers can allocate objects freely and let the new Objective-C compiler manage their deallocation.
Students attending Learning Tree’s Objective-C and iOS programming courses learn about the differences and similarities between their familiar languages and Objective-C. In my experience, even after a few days working with the language, the initial doubts start to disappear and people attending these courses start to embrace the flexibility and power of the language.
If you are interested in learning more about Objective-C, check out Learning Tree’s course entitled Hands On Objective-C Programming Fundamentals for the Mac/iPhone®/iPad®.
1 The C++ Programming Language by Bjarne Stroustrup, Chapter 0, “Notes to the Reader.”