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

NSDictionary,NSMutableDictionary 使用

时间:2015-08-15 18:01:46      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

NSDictionary

 

key --->value
索引 --->内容
存储键值对

 

(1)三种创建方式:

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"Jony" forKey:@"name"];

  

NSArray *keys = @[@"name", @"location"];
NSArray *objects = @[@"Jony", @"China"];
NSDictionary *dict = [NSDictionary dictionaryWithObject:objects forKey:keys];

 

NSDictionary *dict = @{@"name" : @"Jony", @"location", @"China"};

 

(2)调用

id obj = [dict objectForKey:@"name"];

id obj = dict[@"name"];

  

NSMutableDictionary

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setObject:@"Jony" forKey:@"name"];

//deleate
[dict removeObjectForKey:(id)];

NSMutableDictionary *dict = @{@"name" : @"Jony", @"location", @"China"};

  

(1)字典遍历方式

NSArray *keys = [dict allKeys];

for (int i = 0; i < dict.count; i++)
{	
NSString *key = keys[i];
NSString *object = dict[key];

NSLog(@"%@ = %@", key, object);
}

  

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)]
{
NSLog(@"%@ -- %@", key, obj);

//Only oneTime
*stop = YES;
}

  

   

NSDictionary,NSMutableDictionary 使用

标签:

原文地址:http://www.cnblogs.com/fangchun/p/4732686.html

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