Migrating to modern Objective-C

iOS 7 SDK

Last year at WWDC 2012, Apple introduced Modern Objective-C alongside iOS 6. So what’s this? In a few words, it is a huge step forward to help developers write less code to achieve the same things. Worth mentioning is that modern Objective-C has nothing to do with iOS versions, it has to do with the compiler, which means that it is backward compatible.

Let’s dive into the basics of transitioning to modern Objective-C. Xcode offers a refactoring tool (edit>Refactor>Convert to Modern Objective-C syntax) which will convert your project to modern Objective-C (it will also set the appropriate flag in build settings). If you would like to adopt modern Objective-C when writing your code, which is the best, as it is intended to help you write less lines of code, general rules for doing so are the following.

Default property synthesis

Usually iOS developers declare a property in the header file and have to synthesize it in the implementation file in order to access it. With modern Objective-C there is no need to synthesize properties. All properties are synthesized automatically and you can access them using the property name prefixed with an underscore. For example you declare this property in your header (.h) file

In the implementation file you can access your UILabel using:

NSNumber Literals

Up to now, initializing an NSNumber involved a considerable amount of typing and calling class methods to describe the value type you are going to put on that NSNumber. Typical examples are:

In modern Objective-C, the above code looks just like this:

Array Literals

Working with NSArray objects is also simplified in modern Objective-C. The old way of initializing an NSArray was like the below sample code:

Now we can just initialize an NSArray as follows:

Note that in modern Objective-C there is no need to nil terminate an NSArray object. Also when writing the above code, the compiler generates:

The same shortening applies also when accessing an NSArray object. So instead of:

we can now use just:

Dictionary Literals

Initializing an NSDictionary object follows the same concept like NSArray literals. So usually we initialize an NSDictionary like that:

Up to now NSDictionaries needed to be nil terminated like NSArrays. In modern Objective-C the initialization is like the code snippet bellow. Note that now reading an NSDictionary initialization method is also match easier, thanks to the “:” separating each object with its key.

As with NSArrays, when writing this code, the compiler generates:

Accessing values and objects of an NSDictionary is also simplified, so instead of:

we just type:

For a further look on modern Objective-C syntax, you can refer the llvm website here. As seen from these simple code snippets, a lot of common initializations and declarations get much simpler -and actually faster- with the new syntax. It is fair to say that the transition to modern Objective-C is the next big step after moving to ARC for iOS/Mac developers.

2 thoughts on “Migrating to modern Objective-C

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">