Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1700376
  • 博文数量: 174
  • 博客积分: 5493
  • 博客等级: 上校
  • 技术积分: 5802
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-05 15:13
个人简介

炼狱,是为追逐光芒

文章分类

全部博文(174)

文章存档

2017年(1)

2016年(3)

2015年(9)

2014年(5)

2013年(23)

2012年(56)

2011年(45)

2010年(32)

分类: iOS平台

2013-09-30 22:59:26

#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.     NSLog(@"%@->%@",@"k1",[[dict objectForKey:@"k1"] name]);
  10.     //[dict removeAllObjects];
  11.     //[dict removeObjectForKey:@"k1"];
  12.     NSLog(@"retain:%zi",[stu1 retainCount]);
  13.    // NSLog(@"%@",stu1);
  14.     
  15.     //添加其他字典
  16.     NSDictionary *other = [NSDictionary dictionaryWithObject:@"name3" forKey:@"k3"];
  17.     [dict addEntriesFromDictionary:other];
  18.     NSLog(@"count is %zi",[dict count]);
  19.     
  20.     //NSLog(@"%@->%@",@"k3",[dict objectForKey:@"k3"]);  
  21. }


阅读(3243) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~