设为首页 加入收藏

TOP

OC 第七天 字典 lesson7(一)
2015-07-20 17:39:08 来源: 作者: 【 】 浏览:7
Tags:字典 lesson7

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

	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.添加键值对。

	[dicZijie setValue:@"LOL" forKey:@"hoby"];

2.修改key对应的value

	[dicZijie setValue:@"男" forKey:@"hoby"];

3.删除键值对。

	[dicZijie removeObjectForKey:@"sex"];

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

	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-王子杰

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

  	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_

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Collection of algorithm for sor.. 下一篇ACdream区域赛指导赛之手速赛系列..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·「链表」是一种怎样 (2025-12-25 19:20:51)
·C 语言中的链表有哪 (2025-12-25 19:20:48)
·c语言中的链表怎么学 (2025-12-25 19:20:45)
·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)