#pragma mark 创建字典
点击(此处)折叠或打开
1. void dictCreate() {
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k1",
7. @"v2",@"k2",
8. @"v3",@"k3",
9. nil];
10. NSArray *objs = [NSArray arrayWithObjects:@"v1",@"v2",@"v3",nil];
11. NSArray *keys = [NSArray arrayWithObjects:@"k2",@"k3",@"k4",nil];
12. dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
13.
14. NSLog(@"%@",dict);
15. }
将字典写入文件,从文件读取:
点击(此处)折叠或打开
1. void dicUse(){
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k0",
7. @"v2",@"k2",
8. @"v3",@"k3",
9. nil];
10. //返回字典键值对
11. NSLog(@"count=%zi",dict.count);
12.
13. //dictionary不可变,所以只能取值,不能修改
14. id obj = [dict objectForKey:@"k3"];
15. NSLog(@"obj=%@",obj);
16. NSLog(@"%@",dict);
17.
18. //将字典写入文件
19. NSString *path = @"/Users/luteresa/Desktop/dic.txt";
20. //[dict writeToFile:path atomically:YES];
21. dict = [NSDictionary dictionaryWithContentsOfFile:path];
22. NSLog(@"-%@",dict);
23. }
获取多个字典值:
点击(此处)折叠或打开
1. void dictUse2(){
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k0",
7. @"v1",@"k2",
8. @"v3",@"k3",
9. nil];
10. //返回字典键值对
11. NSLog(@"count=%zi",dict.count);
12.
13. //返回所有的key
14. NSArray *keys = [dict allKeys];
15. NSLog(@"keys=%@",keys);
16.
17. //返回所有的value
18. NSArray *objs = [dict allValues];
19. NSLog(@"%@",objs);
20. keys =[dict allKeysForObject:@"v1"];
21. NSLog(@"keys=%@",keys);
22.
23. //根据多个key取出对应的多个value
24. //当key找不到value时,用marker代替
25. objs = [dict objectsForKeys: [NSArray arrayWithObjects:@"k1",@"k2",nil] notFoundMarker:@"h"];
26. NSLog(@"%@",objs);
27.
28. }
遍历字典:
点击(此处)折叠或打开
1. void dicFor(){
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k0",
7. @"v1",@"k2",
8. @"v3",@"k3",
9. nil];
10.
11. for (id key in dict) {
12. id value = [dict objectForKey:key];
13. NSLog(@"%@->%@",key,value);
14. }
15. }
用key迭代器遍历字典:
点击(此处)折叠或打开
1. void dicFor2(){
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k0",
7. @"v1",@"k2",
8. @"v3",@"k3",
9. nil];
10. //key 迭代器
11. NSEnumerator *enumerator = [dict keyEnumerator];
12. id key = nil;
13. while (key = [enumerator nextObject]) {
14. id value = [dict objectForKey:key];
15. NSLog(@"%@->%@",key,value);
16. }
17. //
18. }
用block迭代
点击(此处)折叠或打开
1. void dicFor3(){
2. //NSDictionary是不可变的
3. NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
4. //最常用的初始化方式
5. dict = [NSDictionary dictionaryWithObjectsAndKeys:
6. @"v1",@"k0",
7. @"v1",@"k2",
8. @"v3",@"k3",
9. nil];
10. [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
11. NSLog(@"%@=%@",key,obj);
12. }];
13.
14.
15. }
NSMutableDictionary:字典子集,可变
点击(此处)折叠或打开
1. void dicMutUse(){
2. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
3. Student *stu1 = [Student studentWithName:@"name1"];
4. Student *stu2 = [Student studentWithName:@"name2"];
5.
6. [dict setObject:stu1 forKey:@"k1"];
7. [dict setObject:stu2 forKey:@"k2"];
8. NSLog(@"retain:%zi",[stu1 retainCount]);
9.
10. NSLog(@"%@->%@",@"k1",[[dict objectForKey:@"k1"] name]);
11. //[dict removeAllObjects];
12. //[dict removeObjectForKey:@"k1"];
13. NSLog(@"retain:%zi",[stu1 retainCount]);
14. // NSLog(@"%@",stu1);
15.
16. //添加其他字典
17. NSDictionary *other = [NSDictionary dictionaryWithObject:@"name3" forKey:@"k3"];
18. [dict addEntriesFromDictionary:other];
19. NSLog(@"count is %zi",[dict count]);
20.
21. //NSLog(@"%@->%@",@"k3",[dict objectForKey:@"k3"]);
22. }