Objective-C Literals for iOS in Xcode 4.4

Apple recently released Xcode 4.4, which includes several minor enhancements for iOS developers. One of those features, named Objective-C Literals, is fully supported for OS X desktop apps, but is still a work-in-progress for iOS apps. This “teaser” preview has iOS developers around the world salivating, and for good reason!

Many other languages, such as C#, have syntactic sugar that makes working with common data types easier. Objective-C is now gaining support for language features that eliminate the need for using methods to create and manipulate arrays, dictionaries, and NSValue/NSNumbers (i.e. boxing primitive values into objects).

As of Xcode 4.4 this language feature is not fully baked for iOS apps, as Apple documented in the What’s New in Xcode 4.4 release notes. Regardless of the current limitations, let’s take a quick look at how this great new feature can be used today.

Arrays

The code for creating an NSArray with a pre-defined list of objects is now much less verbose. Refer to Figure 1 to see a comparison of creating an array in C#, old-school Objective-C, and using container literals in Objective-C.

Figure 1 – Creating an immutable array

Initialize an NSArray by including a comma-separated list of objects between @[ and ]. The compiler converts that line of code into a call to the +[NSArray arrayWithObjects:count:] class method. Note, similar to the C# array initialization code, an array created with container literal syntax does not include a nil argument list terminator.

It is not currently possible to create a mutable collection via literal syntax.

Dictionaries

The container literal syntax for creating an NSDictionary is similar to what was seen above. Instead of using brackets, a dictionary is initialized between braces. As seen in Figure 2, the syntax is @{ key : value, key : value }

Figure 2 – Creating an immutable dictionary

My favorite part about this new language feature is that dictionaries can now be used with the standard key/value notation, instead of the bizarre value/key pattern that has plagued NSDictionary for years.

Boxed Numbers

Copying primitive values, such as ints, into an object has always been tedious. This task is generally referred to as boxing. It is usually done because Foundation collections (such as NSArray and NSDictionary) can only store objects, not primitive values. Boxing in C# is taken care of by the compiler, which has pros and cons. In Objective-C the developer must instantiate an NSValue or NSNumber and put a primitive value into it (i.e. “box” it).

The new literal syntax does not remove the need to box values, but it makes the code much more streamlined and readable. As seen in Figure 3, simply put an @ before the literal/constant value, such as @42, and the compiler implicitly wraps that number in the appropriate box type.

Figure 3 – Boxing a number added to an array

Subscripting

Unfortunately, the full feature set of this language enhancement is unavailable for iOS developers, probably until iOS 6 ships. Figure 4 shows that subscripting, which uses array-like syntax to provide easy access to collections, is not yet supported by the compiler.

Figure 4 – Subscripting does not work yet

There is a workaround for this limitation, described in Peter Steinberger’s blog post titled Using subscripting with Xcode 4.4 and iOS 4.3+. The general idea is to include a category on NSObject that declares subscripting methods usable by, but currently unavailable for, iOS apps.

Further reading

For a full explanation of this set of related compiler enhancements, check out the Clang documentation page titled Objective-C Literals. The Big Nerd Ranch published a great blog post about the implications of using Objective-C literals, in the Objective-C Literals, part 2 post.

If you are a .NET developer learning how to write iPhone or iPad apps, check out my book iOS Programming for .NET Developers.

About Josh Smith

Josh Smith is a musician at heart, and a software developer by obsession. He lives in Arizona, plays classical music on the piano, jazz on the guitar, and enjoys brutally long rides on his bicycle. He has a wonderful wife and thinks that, in general, life is good.
This entry was posted in iOS Programming for .NET Developers, Objective-C, Xcode 4. Bookmark the permalink.

One Response to Objective-C Literals for iOS in Xcode 4.4

  1. Pingback: Dew Drop – July 30, 2012 (#1,374) | Alvin Ashcraft's Morning Dew

Comments are closed.