码迷,mamicode.com
首页 > 其他好文 > 详细

Creating Dictionaries

时间:2015-07-23 19:43:03      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

Immutable dictionaries can be defined using the literal @{} syntax. But, like array literals, this was added relatively recently, so you should also be aware of the dictionaryWithObjectsAndKeys: and dictionaryWithObjects:forKeys: factory methods. All of these are presented below.

// Literal syntax
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
// Values and keys as arguments
inventory = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"Mercedes-Benz SLK250",
[NSNumber numberWithInt:22], @"Mercedes-Benz E350",
[NSNumber numberWithInt:19], @"BMW M3 Coupe",
[NSNumber numberWithInt:16], @"BMW X6", nil];
// Values and keys as arrays
NSArray *models = @[@"Mercedes-Benz SLK250", @"Mercedes-Benz E350",
@"BMW M3 Coupe", @"BMW X6"];
NSArray *stock = @[[NSNumber numberWithInt:13],
[NSNumber numberWithInt:22],
[NSNumber numberWithInt:19],
[NSNumber numberWithInt:16]];
inventory = [NSDictionary dictionaryWithObjects:stock forKeys:models];
NSLog(@"%@", inventory);

Creating Dictionaries

标签:

原文地址:http://www.cnblogs.com/qike/p/4671174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!