Picture of Brian Love wearing black against a dark wall in Portland, OR.

Brian Love

NSArray, NSDictionary and NSNumber literals

With the release of Apple’s updates to the LLVM compiler, it is now possible to use object literal notation in Objective-C. For those of us that have become accustom to the object literation notation often used in JavaScript and other language, this is a great step forward. What exactly does this mean? It means you can now declare objects such as NSArray, NSDictionary, and NSNumber using a literal style syntax.

NSNumber

Let’s start with the NSNumber literal notation. Much like the NSString notation, you can now declare an NSNumber using a preceding @ symbol. Objective-C programers are very familiar with declaring an NSString using something like:

NSString *hello = @"Hello, World.";

In this example, we are creating a new instance of an NSString, without the need to allocate and initialize the object, or without using the stringWithFormat: or other NSString class methods. You can also create NSNumber in this same way.

//original numberWithInt: method
NSNumber *twentyNine = [NSNumber numberWithInt:29];
// new literal syntax
NSNumber *twentyNine = @29;

We can also create other NSNumber instances beyond just integers. We can create an unsigned integer, a long, a longlong, floating point numbers, and doubles.

// original syntax
NSNumber *twentyNine = [NSNumber numberWithUnsignedInt:29U];
// literal syntax
NSNumber *twentyNine = @29U;

As you can see, we just declare the unsigned integer with the preceding @ symbol and the trailing “U” type suffix. Here is a quick list of suffixes for NSNumbers:

For another example, let’s create a floating point number and a double:

//original syntax
NSNumber *piAsFloat = [NSNumber numberWithFloat:3.14159F];
// literal syntax
NSNumber *piAsFloat = @3.14159F;

//original syntax
NSNumber *piAsDouble = [NSNumber numberWithDouble:3.14159];
//literal syntax
NSNumber *piAsDouble = @3.14159;

Note in the example above that for a double you do not use a “D” suffix as you might think. If you do, you will get an error when you attempt to compile your application. You can also create an NSNumber from a boolean using the object literal syntax.

//original syntax
NSNumber *true = [NSNumber numberWithBool:YES];
// literal syntax
NSNumber *true = @YES;

NSArray

Although the use of object literals for NSNumber is convenient, I am truly excited about using this new functionality with NSArrays. This is referred to as a container literal. Let’s take a look at the syntax.

// original syntax
NSArray *bandMembers = [NSArray arrayWithObjects:@"Marc", @"Chris", @"Richard", @"Benj", @"Jerry", nil];
// literal syntax
NSArray *bandMembers = @[@"Marc", @"Chris", @"Richard", @"Benj", @"Jerry"];

Notice in the example above that you prepend the @ symbol, followed by the opening square bracket. Then, include you objects that will be added to the array. Also notice that there is no nil at the end of the list of objects, as this is not necessary.

NSDictionary

The syntax for using object literal notation for the NSDictionary class is similar to the syntax used for arrays. This doesn’t save us a lot of time, but it is certainly easier to code, and makes it clear which value belongs to which key, especially if declared on a single line of code.

// original syntax
NSDictionary *bandMembers = [NSDictionary dictionaryWithObjectsAndKeys:
  @"lead singer", @"Marc",
  @"drummer", @"Chris",
  @"lead guitar", @"Richard",
  @"bass guitar", @"Benj",
  @"saxophonist", @"Jerry",
nil];

// literal syntax
NSDictionary *bandMembers = @{
  @"lead singer": @"Marc",
  @"drummer": @"Chris",
  @"lead guitar": @"Richard",
  @"bass guitar": @"Benj",
  @"saxophonist": @"Jerry"
};

This new object literal syntax should come in handy, thanks to the new Clang compiler. You can read the full documentation at:http://clang.llvm.org/docs/ObjectiveCLiterals.html.