通过唯一的key找到相应的value,类似于Map
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", @"性别":@"男"};
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 }
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 = [dic objectForKey:key];
11 NSLog(@"%@ = %@", key, value);
12 }
13 }
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 = [dic objectForKey:key];
13 NSLog(@"%@ = %@", key, value);
14 }
15 }
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 }
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 time
12 NSLog(@"stu1 before dic release:%zi", stu1.retainCount);
13 }
1 NSArray *da = [d allKeys];
1 NSLog(@"%@", d[@"6"]);
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);
[OC Foundation框架 - 10] NSDictionary
原文:http://www.cnblogs.com/wvqusrtg/p/4513069.html