码迷,mamicode.com
首页 > 其他好文 > 详细

OC 第七天 字典 lesson7

时间:2014-09-19 22:37:06      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:c语言   oc   nsmutabledictionary   nsdictionary   

NSDictionary 常用方法


NSDictionary用于保存具有映射关系的数据,因此,NSDictionary集合里保存着两组值:

一组用于保存NSDictionary里的key

另一组用于保存NSDictionary里的value

其中key与value都可以是任何引用类型的数据,key与value之间存在单向一对一的关系。(key不允许重复)

1.创建字典对象;

        NSDictionary * dicYaoGe = [NSDictionary dictionaryWithObjectsAndKeys:@"瑶哥", @"name",
                                   @"?", @"sex",
                                   @"18", @"age",
                                   @"105", @"weight",
                                   nil];
        NSLog(@"%@",dicYaoGe);

注意:此处写法比较反人类,value在前,key在后。

运行结果:

2014-09-19 20:47:06.329 oc07_字典[6837:303] {

    age = 18;

    name = "\U7476\U54e5";

    sex = "?";

    weight = 105;

}

2.获取所有key

        NSLog(@"%@", [dicYaoGe allKeys]);
运行结果:

2014-09-19 20:47:06.473 oc07_字典[6837:303] (

    age,

    sex,

    weight,

    name

)

3.获取所有value

 NSLog(@"%@", [dicYaoGe allValues]);
运行结果:

2014-09-19 20:47:06.474 oc07_字典[6837:303] (

    18,

    "?",

    105,

    "\U7476\U54e5"

)

4.通过key值查询value

<span style="white-space:pre">	</span>NSLog(@"%@",[dicYaoGe objectForKey:@"weight"]);

运行结果:

2014-09-19 20:47:06.474 oc07_字典[6837:303] 105

快速遍历方法:

        for (NSString *key in dicYaoGe) {
            NSLog(@"%@ - %@",key ,[dicYaoGe objectForKey:key]);
        }

运行结果:

2014-09-19 20:47:06.475 oc07_字典[6837:303] age - 18

2014-09-19 20:47:06.476 oc07_字典[6837:303] sex - ?

2014-09-19 20:47:06.476 oc07_字典[6837:303] weight - 105

2014-09-19 20:47:06.477 oc07_字典[6837:303] name - 瑶哥



NSMutableDictionary 常用方法

            NSMutableDictionary *dicZijie = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             @"王子杰",@"name",
                                             @"难以判断",@"sex",
                                             @"七老八十",@"age",
                                             @"200",@"weight",nil];
            for (NSString *key in dicZijie) {
                NSLog(@"%@-%@",key , [dicZijie objectForKey:key]);
            }
运行结果:

2014-09-19 20:57:55.310 oc07_字典[6858:303] age--七老八十

2014-09-19 20:57:55.311 oc07_字典[6858:303] hoby--

2014-09-19 20:57:55.311 oc07_字典[6858:303] weight--200

2014-09-19 20:57:55.312 oc07_字典[6858:303] name--王子杰

1.添加键值对。

<span style="white-space:pre">	</span>[dicZijie setValue:@"LOL" forKey:@"hoby"];

2.修改key对应的value

<span style="white-space:pre">	</span>[dicZijie setValue:@"男" forKey:@"hoby"];

3.删除键值对。

<span style="white-space:pre">	</span>[dicZijie removeObjectForKey:@"sex"];

4.通过for循环遍历所有键值对

<span style="white-space:pre">	</span>for (NSString *key in dicZijie) {
                NSLog(@"%@-%@",key , [dicZijie objectForKey:key]);
            }
运行结果:

2014-09-19 21:00:49.198 oc07_字典[6872:303] age-七老八十

2014-09-19 21:00:49.199 oc07_字典[6872:303] hoby-

2014-09-19 21:00:49.199 oc07_字典[6872:303] weight-200

2014-09-19 21:00:49.200 oc07_字典[6872:303] name-王子杰

对于一个数组中字典的遍历:

  <span style="white-space:pre">	</span>NSDictionary *dicDaGe = [NSDictionary dictionaryWithObjectsAndKeys:@"大哥", @"name",
                                 @"卖药", @"hoby",
                                 @"男", @"sex",nil];
        NSMutableArray *tongJi = [NSMutableArray arrayWithObjects:dicZijie,dicYaoGe,dicDaGe, nil];
        
        for (NSDictionary *dic in tongJi) {
            NSLog(@"______________");
            for (NSString *key in dic) {
                NSLog(@"%@--%@",key,[dic objectForKey:key]);
            }
        }
运行结果:

2014-09-19 21:00:49.201 oc07_字典[6872:303] ______________

2014-09-19 21:00:49.201 oc07_字典[6872:303] age--七老八十

2014-09-19 21:00:49.201 oc07_字典[6872:303] hoby--

2014-09-19 21:00:49.202 oc07_字典[6872:303] weight--200

2014-09-19 21:00:49.202 oc07_字典[6872:303] name--王子杰

2014-09-19 21:00:49.203 oc07_字典[6872:303] ______________

2014-09-19 21:00:49.203 oc07_字典[6872:303] age--18

2014-09-19 21:00:49.204 oc07_字典[6872:303] sex--?

2014-09-19 21:00:49.204 oc07_字典[6872:303] weight--105

2014-09-19 21:00:49.205 oc07_字典[6872:303] name--瑶哥

2014-09-19 21:00:49.205 oc07_字典[6872:303] ______________

2014-09-19 21:00:49.206 oc07_字典[6872:303] name--大哥

2014-09-19 21:00:49.206 oc07_字典[6872:303] hoby--卖药

2014-09-19 21:00:49.207 oc07_字典[6872:303] sex--







OC 第七天 字典 lesson7

标签:c语言   oc   nsmutabledictionary   nsdictionary   

原文地址:http://blog.csdn.net/qq11231325/article/details/39402781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!