首页 > 代码库 > [OC Foundation框架 - 10] NSDictionary

[OC Foundation框架 - 10] NSDictionary

通过唯一的key找到相应的value,类似于Map

NSDictionary是不可变的
 
1.创建
 1 void dicCreate() 2 { 3     //Immutable 4 //    NSDictionary *dic = [NSDictionary dictionary]; 5     6     NSDictionary *dic = [NSDictionary dictionaryWithObject:@"Simon" forKey:@"name"]; 7     8     dic = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil]; 9    10     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];11     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];12     dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];13    14     NSLog(@"%@", dic);15    16 }
 
集中批量创建字典
1         NSDictionary *d11_1 = @{@"姓名":@"张三", @"年龄":@"21", @"性别":@""};
 
若存在同名的元素,采用最先定义的,多余的元素不会被计数
不允许nil作为键值
NSMutableDictioinary不能使用此方法,因为返回的是一个NSDictionary
 
 
2.基本操作
(1)键值对数量
(2)比较
(3)取值
(4)IO操作
 1 void dicUse() 2 { 3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil]; 4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; 5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; 6     7     NSLog(@"%@", [dic objectForKey:@"k1"]); 8 } 9  10 void dicUse2()11 {12     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v1", @"v2", nil];13     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];14     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];15    16     NSArray *keys2 = [dic allKeys];17     NSLog(@"%@", keys2);18    19     NSArray *keys3 = [dic allKeysForObject:@"v1"];20     NSLog(@"%@", keys3);21    22    23     NSArray *objs2 = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4",nil] notFoundMarker:@"not found"];24     NSLog(@"%@", objs2);25 }
 
3.遍历
 1 void dicLoop() 2 { 3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil]; 4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; 5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; 6     7     //Loop all keys in dictionary 8     for (id key in dic) 9     {10         id value =http://www.mamicode.com/ [dic objectForKey:key];11         NSLog(@"%@ = %@", key, value);12     }13 }
 
4.迭代器
 1 void dicEnumerator() 2 { 3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil]; 4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; 5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; 6     7     //key enumerator 8     NSEnumerator *enumerator = [dic keyEnumerator]; 9     id key =nil;10     while (key = [enumerator nextObject])11     {12         id value =http://www.mamicode.com/ [dic objectForKey:key];13         NSLog(@"%@ = %@", key, value);14     }15 }
 
5.block迭代
 1 void dicBlockLoop() 2 { 3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil]; 4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; 5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys]; 6     7     [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 8         NSLog(@"%@ - %@", key, obj); 9     }];10 }
 
6.内存管理
 1 void memoryManage() 2 { 3     Student *stu1 = [Student studentWithName:@"Simon"]; 4     Student *stu2 = [Student studentWithName:@"Joke"]; 5     Student *stu3 = [Student studentWithName:@"Man"]; 6     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:stu1,@"stu1", 7                                                                     stu2, @"stu2", 8                                                                     stu3, @"stu3", nil]; 9    10    11      //When dictionary is destroyed, the keys & objects will be released one time12     NSLog(@"stu1 before dic release:%zi", stu1.retainCount);13 }
 
7.取出键、值
(1)
1         NSArray *da = [d allKeys];
(2)
1         NSLog(@"%@", d[@"6"]);
 
8.NSDictionary是无序的,取出来的值不会按照存入的顺序排列
1         NSMutableDictionary *d = [NSMutableDictionary dictionary];2         for (int i=0; i<100; i++)3         {4             [d setObject:@"abc" forKey:[NSString stringWithFormat:@"%d", i]];5         }6        7         NSLog(@"%@", d);
 
out:
2014-11-19 02:36:25.850 03-NSArray[5171:303] {
    0 = abc;
    1 = abc;
    10 = abc;
    11 = abc;
...
 
 
 

[OC Foundation框架 - 10] NSDictionary