There’s no reason to be scared of brackets – Objective-C from a Java developers point of view

I’ve been a Java guy for quite some time now. I think i know the language pretty well, even though I’ve used it almost exclusively for Android development. Recently though I had the time and opportunity to iOS development – something that was on top of my programming TODO list. This also meant that I had to familiarize myself with a new language – Objective-C. That was fine with me – I enjoy discovering new stuff. I won’t talk about how it was to learn Objective-C as a Java developer, instead i’ll focus on some nice features of Objective-C, which I found very useful.

Pointer to ‘nil’

I would like to illustrate one important concept when it comes to how the two languages treat pointers/references to nil/null.

Lets assume that the class Cat exists and has a purr method. What is the most important difference here? When ran, the java program will terminate with the following exception:

The Objective-C code however will be executed without any hiccups.

I was really surprised when I’ve first seen this behaviour, but it wasn’t long until I began to make use of that fact.

A trivial example of making use of this is when you want to call a method on an object that you’ve been passed from somewhere and you’re not guaranteed that the reference/pointer is properly set.

Of course this only makes sense if the rest of the code does not depend on the reference/pointer being valid.

Looks great right? Well… there’s one downside to this. It’s harder to find bugs. You will not receive any warning, exception or message telling you that the method failed to run. This can be tricky for new Objective-C developers, simply because you tend to forget about this feature when debugging your code. So remember to check for nil pointers when some things aren’t going the way you’ve planned!

How does this work you might ask? Well, the syntax that you’re seeing in the Objective-C snippet isn’t in reality a regular function call (like in Java or C). Instead, the bracket syntax means that you are sending the message purr to the object that the cat pointer currently points to. I won’t go into more detail, but the whole mechanism of sending messages is not only apparent in this situation but it’s used throughout the language.

Optional protocol methods

Probably one of my favourite features of Objective-C. Protocols (interfaces) can have optional methods. There’s not much to explain here. It’s a very simple concept, but it’s something that’s not present in Java.

As a quick example let’s take a look at a commonly used interface in Android – TextWatcher.

Most of the time, I only needed to implement one of these methods. Unfortunately, in Java you are forced to implement the whole interface, even if you’re only going to use one method and leave the others empty.

In Objective-C on the other hand, protocol methods can be marked with the @optional directive, which results in the obvious – makes the implementation of that method optional.

As you can see in the example above, by default, methods in a protocol are required. You can use the @optional directive to mark all the methods that follow as optional methods. There’s also the @required directive which works as you’d expect.

Now this isn’t anything big – it doesn’t bring anything new to the table. All it does is make your code cleaner. This feature however has been put to good use within the UIKit framework, where it is used extensively. A great example is the delegation pattern used throughout UIKit. Let’s take a look at UITableViewDelegate for example:

There’s ~33 methods in this protocol. Without a doubt, doing this the same way in Java would be a very bad idea :) .

Categories

The name probably doesn’t describe this feature too well, nevertheless this is something that you’ll love as soon as you learn how to use it. Especially if you are purely a Java developer, where such functionality is not present.

In a nutshell, categories let you add new methods to existing classes. Again – something small but very nice to have at your disposal.

As usual, lets look at an example:

Above, we’ve created a new category for the UIColor class. We have defined some new colors, which can now be accessed the same way as the predefined colors (i.e. [UIColor redColor]). Let’s see how we can use our new category.

Pretty much straightforward. The only difference here is that you must import UIColor+MyCustomColors.h, as it contains the declarations of your new methods.

There’s much, much more to say about Objective-C, I’ve only scratched the surface in this article. If I got you interested then I recommend that you pick up a book and start learning Objective-C. Personally, I would recommend “Learn Objective-C for Java Developers” (http://www.apress.com/9781430223696). It covers the differences between these languages quite well. To sum it all up, I think that Objective-C offers more flexibility than Java, which allows developers to design and implement a clean architecture, which is easy to use and maintain, but in order to use it efficiently you need to familiarize yourself with the language first. Simply moving concepts over from Java to Objective-C will not get you far because the languages are quite different from eachother.

 

Podziel się tym artykułem