Brief Intro to Container Literals of Foundation Framework

Container Literals

Objective-C provides support for the creation of a variety of container literals; specifically, literal notation for arrays and dictionaries (i.e., NSArray and NSDictionary objects).

NSArray Literals

Chapter 10 provided a brief overview of NSArray literals. This notation simplifies the creation ofNSArray instances. The syntax for an array literal is

@[firstObj, secondObj, ...]

An NSArray literal begins with an ampersand (@) symbol, followed by a comma-separated list of objects enclosed in brackets. The list of objects is not terminated with a nil. As with the NSArrayAPIs, the objects of the array must be Objective-C object pointer types, including other Objective-C Literals (NSNumber literals, NSArray literals, etc.). Listing 16-3 modifies the example shown in Listing 16-2, this time using array literal notation to create an NSArray object.

Listing 16-3.  Creating an NSArray Literal

NSArray *numbers = @[@'A', @YES, @-3, @21U, @250L,
                    @9876543210LL, @3.14F, @-52.687];

Array literals can be nested, thereby enabling the creation of arrays within arrays. Listing 16-4demonstrates the creation of a nested NSArray literal.

Listing 16-4.  Nested NSArray Literals

NSArray *groups = @[@[@0, @1, @2], @[@"alpha", @"beta"]];

As the previous examples demonstrate, NSArray literal notation both simplifies the creation of arrays and reduces programmer errors.

The compiler translates each NSArray literal into the NSArray convenience constructorarrayWithObjects:count:. This method is invoked at runtime to create the corresponding NSArrayinstance. In effect, the NSArray literal

@[@"Hello", @"World"];

is equivalent to the code fragment shown in Listing 16-5.

Listing 16-5.  Creating an NSArray Instance (Without Array Literal Notation)

NSString *strings[2];
strings[0] = @"Hello";
strings[1] = @"World";
[NSArray arrayWithObjects:strings count:2];

Array literal notation is used to create immutable array instances. You can create a mutable array (e.g., an NSMutableArray) from an array literal by sending it a mutableCopy message, as follows.

NSMutableArray *mutableWords = [@[@"Hello", @"World"] mutableCopy];

NSDictionary Literals

Objective-C also provides support for creating NSDictionary literals. The syntax for a dictionary literal is

@{keyObj:valueObj, ...}

An NSDictionary literal begins with an ampersand symbol, followed by one or more comma-separated key-value pairs, all enclosed in braces. For each key-value pair, a colon is placed between the key and its corresponding value. Each key must be an object pointer type that conforms to the NSCopyingprotocol, and each value must be an object pointer typeNeither keys nor values in a dictionary literal can have a nil value; NSNull must be used to represent null objects in a containerListing 16-6 creates a simple dictionary literal with two entries—an order for one cheeseburger and an order for two hot dogs.

Listing 16-6.  Creating an NSDictionary Literal

NSDictionary *orders = @{@1111:@"1 Cheeseburger",
                         @1112:@"2 Hot dogs"};

Notice that the NSDictionary literal syntax for key-value pairs is key:value, whereas the standardNSDictionary APIs use a value, key syntax for creating a dictionary instance (e.g., the NSDictionary method dictionaryWithObjects:forKeys:). As with array literals, dictionary literals can be nested; that is, the value in the key-value pair can be a container instance (including another NSDictionary literal). Listing 16-7 demonstrates the creation of a nested NSDictionary literal (currentOrders) with two entries, where the value for each entry is an NSDictionary literal.

Listing 16-7.  Nested NSDictionary Literal

NSDictionary *order1 = @{@1111:@"1 Cheeseburger",
                         @1112:@"2 Hot dogs"};
NSDictionary *order2 = @{@1113:@"1 large cheese pizza"};
NSDictionary *currentOrders = @{@"table1":order1, @"table2":order2};

The compiler translates each NSDictionary literal expression into the NSDictionary convenience constructor dictionaryWithObjects:forKeys:count:. This method is invoked at runtime to create the corresponding NSDictionary instance—in effect, the following dictionary literal:

@{@1111:@"1 Cheeseburger"}

is equivalent to the code fragment shown in Listing 16-8.

Listing 16-8.  Creating an NSDictionary Instance (Without Dictionary Literal Notation)

NSString *values[1];
values[0] = @"1 Cheeseburger";
NSNumber *keys[1];
keys[0] = @1111;
[NSDictionary dictionaryWithObjects:values forKeys:keys count:1];

Dictionary literal notation is used to create immutable dictionary instances. As specified with array literals, you can create a mutable dictionary (e.g., an NSMutableDictionary) from a dictionary literal by sending it a mutableCopy message.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值