Objective-C语法之NSDictionary和NSMutableDictionary

Java 有Map,可以把数据以键值对的形式储存起来,取值的时候通过key就可以直接拿到对应的值,方便快捷。在Objective-C语言中,词典就是做这样的事情的,和NSArray一样,一个词典对象也能保存不同类型的值,词典也分别有不可变词典和可变的词典(NSDictionary与 NSMutableDictionary),前者是线程安全的,后者不是。

PS:跟数组的区别是:词典乱序存储数据,例如通过[词典对象实例 allKeys]获取的数组对象实例是乱序的,这时需要通过[数组对象实例 sortedArrayUsingComparator:自定义排序比较器]来进一步自定义排序。

1、不可变词典NSDictionary的主要用法:

[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对直接创建词典对象,结尾必需使用nil标志结束。

[dictionary count]: 得到词典的键值对数量。
[dictionary keyEnumerator]: 将词典的所有key储存在NSEnumerator中,乱序;类似于Java语言中的迭代器。
[dictionary objectEnumerator]: 将词典的所有value储存在NSEnumerator中;乱序。
[dictionary objectForKey:key]: 通过传入key对象可以拿到当前key对应储存的值。

 

2、可变的词典NSMutableDictionary。

NSMutableDictionary是NSDictionary的子类,所以继承了NSDictionary的方法, 以上的代码对NSMutableDictionary来说完全可用。我们试试不一样的地方

增删键值数据。

[dictionary setObject: forKey:] :向可变的词典动态的添加数据 
[dictionary removeAllObjects..] : 删除掉词典中的所有数据。
[dictionary removeObjectForKey..] :删除掉词典中指定key的数据

代码示例:

main.m

 1 #import <Foundation/Foundation.h>
 2 /**
 3  NSDictionary 不可变词典,NSEnumerator对应的Value(objectEnumerator)/Key(keyEnumerator)枚举器是乱序的
 4  */
 5 void testDictionary() {
 6     NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"某某人", @"ChineseName", @"KK", @"EnglishName", @"18", @"Age", nil]; //Value and Key, don't ignore match case
 7     NSLog(@"[dictionary count]=%d", (int)[dictionary count]);
 8     
 9     NSEnumerator *enumeratorKey = [dictionary keyEnumerator];
10     for (NSObject *obj in enumeratorKey) {
11         NSLog(@"dictionary Key=%@, Value=%@", obj, [dictionary objectForKey:obj]);
12     }
13     
14     NSEnumerator *enumeratorValue = [dictionary objectEnumerator];
15     for (NSObject *obj in enumeratorValue) {
16         NSLog(@"dictionary Value=%@", obj);
17     }
18     
19     //进行根据Key的降序排序
20     NSArray *array = [dictionary allKeys];
21     array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
22         return [obj1 compare:obj2] == NSOrderedDescending; //obj1是从最后的数组元素开始;最终结果是A-Z,1-9;相当于实际的升序排序
23     }];
24     for (NSObject *obj in array) {
25         NSLog(@"dictionary Key=%@, Value=%@", obj, [dictionary objectForKey:obj]);
26     }
27 }
28 /**
29  NSMutableDictionary 可变词典,NSEnumerator对应的Value(objectEnumerator)/Key(keyEnumerator)枚举器是乱序的
30  */
31 void testMutableDictionary() {
32     NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"某某人", @"ChineseName", @"KK", @"EnglishName", @"18", @"Age", nil]; //Value and Key, don't ignore match case
33     [dictionary2 setObject:@"Male" forKey:@"Gender"];
34     NSLog(@"[dictionary2 count]=%d", (int)[dictionary2 count]);
35     
36     NSEnumerator *enumeratorKey = [dictionary2 keyEnumerator];
37     for (NSObject *obj in enumeratorKey) {
38         NSLog(@"dictionary2 Key=%@, Value=%@", obj, [dictionary2 objectForKey:obj]);
39     }
40     
41     NSEnumerator *enumeratorValue = [dictionary2 objectEnumerator];
42     for (NSObject *obj in enumeratorValue) {
43         NSLog(@"dictionary2 Value=%@", obj);
44     }
45     
46     [dictionary2 removeObjectForKey:@"EnglishName"];
47     NSLog(@"[dictionary2 count]=%d, after the operation of removeObjectForKey", (int)[dictionary2 count]);
48     
49     [dictionary2 removeAllObjects];
50     NSLog(@"[dictionary2 count]=%d, after the operation of removeAllObjects", (int)[dictionary2 count]);
51 }
52 int main(int argc, const char * argv[]) {
53     @autoreleasepool {
54         testDictionary();
55         
56         testMutableDictionary();
57     }
58     return 0;
59 }

 

结果:

 

 1 2015-03-16 00:36:41.146 OCNSDictionary[787:35149] [dictionary count]=3
 2 2015-03-16 00:36:41.150 OCNSDictionary[787:35149] dictionary Key=EnglishName, Value=KK
 3 2015-03-16 00:36:41.150 OCNSDictionary[787:35149] dictionary Key=Age, Value=18
 4 2015-03-16 00:36:41.150 OCNSDictionary[787:35149] dictionary Key=ChineseName, Value=某某人
 5 2015-03-16 00:36:41.150 OCNSDictionary[787:35149] dictionary Value=KK
 6 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary Value=18
 7 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary Value=某某人
 8 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary Key=Age, Value=18
 9 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary Key=ChineseName, Value=某某人
10 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary Key=EnglishName, Value=KK
11 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] [dictionary2 count]=4
12 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary2 Key=Age, Value=18
13 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary2 Key=Gender, Value=Male
14 2015-03-16 00:36:41.151 OCNSDictionary[787:35149] dictionary2 Key=EnglishName, Value=KK
15 2015-03-16 00:36:41.152 OCNSDictionary[787:35149] dictionary2 Key=ChineseName, Value=某某人
16 2015-03-16 00:36:41.158 OCNSDictionary[787:35149] dictionary2 Value=18
17 2015-03-16 00:36:41.159 OCNSDictionary[787:35149] dictionary2 Value=Male
18 2015-03-16 00:36:41.159 OCNSDictionary[787:35149] dictionary2 Value=KK
19 2015-03-16 00:36:41.159 OCNSDictionary[787:35149] dictionary2 Value=某某人
20 2015-03-16 00:36:41.160 OCNSDictionary[787:35149] [dictionary2 count]=3, after the operation of removeObjectForKey
21 2015-03-16 00:36:41.160 OCNSDictionary[787:35149] [dictionary2 count]=0, after the operation of removeAllObjects

 

posted @ 2015-06-13 20:07  KenmuHuang  阅读(682)  评论(0编辑  收藏  举报
如果您看完本篇博文,觉得对您有所收获,请点击右下角的 [推荐]
如果您想转载,请注明出处(原创内容,请尊重个人劳动成果)
如果您有任何意见或建议,欢迎留言
感谢您的阅读,敬请关注我的后续博客文章