Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Object literal syntax in Objective C 3

Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
I wrote this info in a post in the development board, but it occurred to me it might be useful here:

Objective C version 3 added Object literals, a way to declare objects with a very compact syntax.

Check out this link to the website for more info:

The summary is this:

Before Objective C literals, you had to declare NSNumbers like this:

NSNumber aNumber = [NSNumber numberWithInteger: 42];
With Objective C literals, you can do the same thing by putting the number in parenthesis and preceding it with an "@"
NSNumber aNumber = @(42);

Likewise, with arrays, the old way to create and populate an array was like this:
NSArray *anArray = [NSArray arrayWithObjects: 

The new syntax would look like this:
NSArray *anArray = @[

The old way to fetch an item from an array:
id anObject = [anArray objectAtIndex: 1];

New syntax:
id anObject = anArray[1];

The old way to replace an item in a mutable array:
[anArray replaceObjectAtIndex: 0 withObject: @"new object"];

New syntax:
anArray[0] = @"new object";

(Note that the above will crash if you try to add an item with an index that would leave missing items in the array)

Dictionaries also support new syntax. The old way:
NSDictionary *aDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  @"value1", @"key1", 
  @"value2", @"key2", 
  @"value3", @"key3", 
The above syntax is counter-intuitive, since you have to provide an even number of objects, with the value before the key.

The new syntax:
NSDictionary *aDictionary = @{
  @"key1": @"value1",
  @"key2": @"value2",
  @"key3": @"value3"
In the new syntax you use pairs of objects separated by colons


Which is cleaner syntactically.

There is also new syntax for fetching an item from a dictionary:

The old way:
id anObject = [aDictionary objectForKey: @"key1"];
New way:
id anObject = aDictionary[@"key1"];

And new syntax for setting an object in a mutable dictionary. Old way:
[aDictionary setObject: @"new object" forKey: @"key1"];
new way:
aDictionary[@"key1"] = @"new object;

For all collections, the new syntax for initializing a collection not require a terminating nil, where the new syntax did.

Some language purists don't like these new syntax changes because when you look at code, it's hard to tell if the code is using Objective C literals or C array/structures. (The syntax is identical between NSArray indexing and C array indexing, and the dot syntax for reading/saving properties is identical to the C syntax for reading and assigning elements to C structures.

Personally, I love the new syntax. It's much cleaner and easier to type and to read. I don't mind looking at the types of the variables to tell if this is C or Objective C code.
Duncan C

Animated GIF created with Face Dancer, available for free in the app store.

I'm available for one-on-one help at CodeMentor


Sign In or Register to comment.