OC 字典

来源:互联网 发布:淘宝运营培训 编辑:程序博客网 时间:2024/06/02 08:54

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--







0 0