首页 > 其他 > 详细

Creating Dictionaries

时间:2015-07-23 19:42:31      阅读:109      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!