Container Literals

Container literals add another great language feature to the LLVM Clang compiler. Prior to its addition, you had to create dictionaries and arrays as shown in the following snippet, which creates a three-item array and a three-key dictionary:

NSArray *array = [NSArray arrayWithObjects: @"one", @"two", @"three", nil]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:                               @"value 1", @"key 1",                               @"value 2", @"key 2",                               @"value 3", @"key 3",                               nil];

These forms are wordy and require a nil terminator. That’s not always a bad thing, but certainly it’s a thing that’s easy to forget ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.