NSDictionary Literals In Objective-C

Mon, Aug 20

I’ve previously written about NSNumber and NSArray literals, let’s have a look at NSDictionary literals.

Before we get started, a quick note, to use literals you will need to be running Xcode 4.4 or greater as well as the Apple LLVM compiler 4.0 or greater.

As with NSString objects, which allow definition using literal syntax:

NSString *str = @"Some string";

you can now create NSDictionary objects using literals. A range of examples follow, showing the old style definition as well as the new literal syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Create empty dictionary
NSDictionary *emptyDictOld = [NSDictionary dictionary];
// New literal style
NSDictionary *emptyDictNew = @{};
 
// Create dictionary, one key and one object
NSDictionary *dictOneKeyOneObjectOld = 
   [NSDictionary dictionaryWithObject:@"StringObject" forKey:@"Key"];
// New literal style * notice key-object (not object-key)
NSDictionary *dictOneKeyOneObjectNew = @{@"Key" : @"StringObject"};
 
// Create dictionary, multiple keys and objects
NSDictionary *dictKeysObjectsOld = [NSDictionary dictionaryWithObjectsAndKeys:
                      // Object and key pairs
                      @"Beer", @"DrinkKey",
                      @"Stout", @"StyleKey",
                      @"Jet Black", @"ColorKey",
                      nil];
 
// New literal style * notice key-object (not object-key)
NSDictionary *dictKeysObjectsNew = @{@"DrinkKey" : @"Beer",
                      @"StyleKey" : @"Stout",
                      @"ColorKey" : @"Jet Black"};

Notice on line 10 the definition is now the key, followed by a colon, followed by the value – a much more intuitive way to define key-value pairs.

Beyond the obvious shorthand notation of literals, with the old format when defining multiple entries, it was required to end the definition with a nil (see line 18), no more with the literal syntax.

Expanding literals to include arrays, dictionaries and number objects, is a wonderful addition to Objective-C. Thanks Apple.

7 comments

Pretty cool. I look forward to writing

NSDictionary *dict = @{
@”FavoriteOS” : @”Dick’s Operating System”,
@”FavoriteDrink” : @”Miks Klim”
};

by Todd Lehman on Aug 21, 2012. Reply #

And don’t forget:

NSDictionary *dict = @{
@”FavoriteStore” : @”Bob’s Pretty Good Computer Store”,
@”RecentAction” : @”Krap the rac”
};

by John Muchow on Aug 24, 2012. Reply #

Thank you so much for nice tips!

by Jonghyun Kim on Mar 26, 2013. Reply #

Can I replace objectAtKey with some literals?
New way could short also reading instructions…

by Fabiosoft on Mar 26, 2013. Reply #

Sure, you could use array literals to add an object: NSArray literals in Objective-C

by John Muchow on Mar 26, 2013. Reply #

can we use literal syntax to create dynamically allocated objects? If yes can you write the syntax?

by RizITN on Mar 27, 2013. Reply #

The code in lines 21-23 dynamically allocates a dictionary.

by John Muchow on Mar 27, 2013. Reply #

Leave a Comment