// 字典
// 1.创建字典对象
// NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
// NSMutableDictionary *dic1 =[NSMutableDictionary dictionary];
// 便利构造器建立字典
// NSMutableDictionary *dic3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4",nil];
// 正常法建立字典
// NSMutableDictionary *dic2 =[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1",@"2",@"3",@"4", nil];
// NSLog(@"%@",dic2);
/* 结果
2015-07-22 19:33:47.371 OC练习[1532:131248] {
2 = 1;
4 = 3;
}
*/
// key是唯一的,但是value可以重复
// key不能为空
// 2. .count当前有多少键值对
// NSLog(@"%ld",dic2.count);
// 结果:2015-07-22 19:36:15.450 OC练习[1540:131959] 2
// 3.通过key找到对应的value
// NSLog(@"%@",[dic2 objectForKey:@"4"]);
// 或者 NSLog(@"%@",dic[@"4"]);
// 结果 :2015-07-22 19:39:31.709 OC练习[1551:132735] 3
// 4.获取字典所有的key和所有的value
// allValues和allKeys可以用点语法,而且可以用数组
// NSLog(@"%@",[dic2 allKeys]);
// NSLog(@"%@",[dic2 allValues]);
// 或者 NSLog(@"%@",dic2.allValues);
// 也可以写成数组形势 NSLog(@"%@",dic.allValues[1]);
/* 结果
2015-07-22 19:56:21.814 OC练习[1673:137865] (
2,
4)
2015-07-22 19:57:41.583 OC练习[1681:138310] (
1,
3)
*/
// 5.用for遍历字典
// NSMutableDictionary *dic1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4",nil];
// NSMutableArray *dic2=[NSMutableArray arrayWithObjects:dic1, nil];
// forin 的前部分是字典里的key
// for (NSString *str in dic1) {
// NSLog(@"%@", str);
// }
// 找到字典里的value
// for (NSString *str in dic1) {
// NSLog(@"%@", dic1[str]);
// }
// 也可以写成
//for (int i =0; i < dic1.allValues.count; i++) {
// NSLog(@"%@",dic1.allValues[i]);
// }
/* 结果
2015-07-22 19:56:21.814 OC练习[1673:137865] (
2,
4)
2015-07-22 19:57:41.583 OC练习[1681:138310] (
1,
3)
2015-07-22 19:57:41.583 OC练习[1681:138310] (
1,
3)
*/
// NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4",@"5",@"6", nil];
// 6.setObject:对可变字典,当前的key不存在时,进行键值的添加;存在时,对值的修改
// [dic setObject:@"7" forKey:@"8"];
// [dic setObject:@"1000" forKey:@"8"];
// NSLog(@"%@",dic);
/* 结果
2015-07-22 20:43:00.910 OC练习[1773:150445] {
2 = 1;
4 = 3;
6 = 5;
8 = 1000;
}*/
// setObject是字典自己的方法,setValue是KVC的方法,即时效果一样,但以后要对字典进行操作,还是用setObject
// [dic setValue:@"100" forKey:@"nihao"];
// NSLog(@"%@",dic);
/* 结果
2015-07-22 20:46:25.407 OC练习[1797:151760] {
2 = 1;
4 = 3;
6 = 5;
nihao = 100;
}*/
// 7.移除键值对
// [dic removeObjectForKey:@"6"];
// NSLog(@"%@",dic);
/* 结果
2015-07-22 20:44:07.919 OC练习[1789:151023] {
2 = 1;
4 = 3;
}*/
// 8.全部移除
// [dic removeAllObjects];
// NSLog(@"%@",dic);
// 9.1字典里面嵌套数组
// NSArray *arr1 =@[@"1",@"2",@"3",@"4"];
// NSArray *arr2 =@[@"5",@"6",@"7",@"8"];
// NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"arr1",arr2,@"arr2", nil];
// 遍历 1
// for (NSString *str in dic) {
// NSLog(@"%@",dic[str]);
// }
/* 结果
2015-07-22 20:48:59.906 OC练习[1806:152676] (
1,
2,
3,
4
)
2015-07-22 20:48:59.907 OC练习[1806:152676] (
5,
6,
7,
8
)
*/
// 遍历 2
// for (NSString *temp in dic) {
// 针对对应的key找到value,value是一个数组,所以用一个数组对象来接收
// NSArray *arr=dic[temp];
// for (NSString *temp in arr) {
// NSLog(@"%@",temp);
//
// }
// }
/* 结果
2015-07-22 20:52:29.288 OC练习[1814:153478] 1
2015-07-22 20:52:29.288 OC练习[1814:153478] 2
2015-07-22 20:52:29.289 OC练习[1814:153478] 3
2015-07-22 20:52:29.289 OC练习[1814:153478] 4
2015-07-22 20:52:29.289 OC练习[1814:153478] 5
2015-07-22 20:52:29.289 OC练习[1814:153478] 6
2015-07-22 20:52:29.289 OC练习[1814:153478] 7
2015-07-22 20:52:29.289 OC练习[1814:153478] 8
*/
// 9.2数组里对象是字典
// NSDictionary *dic1 =[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
// NSDictionary *dic2 =[NSDictionary dictionaryWithObjectsAndKeys:@"liushanshan",@"name",@"20",@"age" ,nil];
// NSDictionary *dic3 =[NSDictionary dictionaryWithObjectsAndKeys:@"liuxingyu",@"name",@"21",@"age", nil];
// NSDictionary *dic4 =[NSDictionary dictionaryWithObjectsAndKeys:@"shangshuai",@"name",@"21",@"age" ,nil];
// NSArray *arr=@[dic1,dic2,dic3,dic4];
// 遍历
// for (NSDictionary *dic in arr) {
// for (NSString *str in dic) {
// NSLog(@"%@",dic[str]);
// }
// }
/* 结果
2015-07-22 11:17:29.152 OC06_字典[942:53788] 杨林
2015-07-22 11:17:29.153 OC06_字典[942:53788] 19
2015-07-22 11:17:29.153 OC06_字典[942:53788] liushanshan
2015-07-22 11:17:29.153 OC06_字典[942:53788] 20
2015-07-22 11:17:29.153 OC06_字典[942:53788] liuxingyu
2015-07-22 11:17:29.153 OC06_字典[942:53788] 21
2015-07-22 11:17:29.154 OC06_字典[942:53788] shangshuai
2015-07-22 11:17:29.154 OC06_字典[942:53788] 21
*/
// 9.3字典里有数组,数组里的对象是字典
// NSDictionary *dic1 =[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
// NSDictionary *dic2 =[NSDictionary dictionaryWithObjectsAndKeys:@"liushanshan",@"name",@"20",@"age" ,nil];
// NSDictionary *dic3 =[NSDictionary dictionaryWithObjectsAndKeys:@"liuxingyu",@"name",@"21",@"age", nil];
// NSDictionary *dic4 =[NSDictionary dictionaryWithObjectsAndKeys:@"shangshuai",@"name",@"21",@"age" ,nil];
// NSArray *arr1=@[dic1,dic2];
// NSArray *arr2 =@[dic3,dic4];
// NSDictionary *bigDic =[NSDictionary dictionaryWithObjectsAndKeys:arr1,@"arr1",arr2,@"arr2", nil];
//
// for (NSArray *tempArr in bigDic) {
// for (NSDictionary *dic in bigDic[tempArr]) {
// for (NSString *key in dic) {
// NSLog(@"%@",dic[key]);
// }
// }
// }
/* 结果
2015-07-22 21:00:10.382 OC练习[1824:155605] 杨林
2015-07-22 21:00:10.383 OC练习[1824:155605] 19
2015-07-22 21:00:10.383 OC练习[1824:155605] liushanshan
2015-07-22 21:00:10.383 OC练习[1824:155605] 20
2015-07-22 21:00:10.383 OC练习[1824:155605] liuxingyu
2015-07-22 21:00:10.383 OC练习[1824:155605] 21
2015-07-22 21:00:10.384 OC练习[1824:155605] shangshuai
2015-07-22 21:00:10.384 OC练习[1824:155605] 21
*/
// 9.4数组里有数组,数组里有字典
// NSDictionary *dic1 =[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
// NSDictionary *dic2 =[NSDictionary dictionaryWithObjectsAndKeys:@"liushanshan",@"name",@"20",@"age" ,nil];
// NSDictionary *dic3 =[NSDictionary dictionaryWithObjectsAndKeys:@"liuxingyu",@"name",@"21",@"age", nil];
// NSDictionary *dic4 =[NSDictionary dictionaryWithObjectsAndKeys:@"shangshuai",@"name",@"21",@"age" ,nil];
// NSArray *arr1=@[dic1,dic2];
// NSArray *arr2 =@[dic3,dic4];
// NSArray *bigArr=@[arr1,arr2];
//
// for (NSArray *arr in bigArr) {
// for (NSDictionary *dic in arr ) {
// for (NSString *key in dic) {
// NSLog(@"%@",dic[key]);
// }
// }
// }
/* 结果
2015-07-22 21:03:28.792 OC练习[1832:156645] 杨林
2015-07-22 21:03:28.793 OC练习[1832:156645] 19
2015-07-22 21:03:28.793 OC练习[1832:156645] liushanshan
2015-07-22 21:03:28.793 OC练习[1832:156645] 20
2015-07-22 21:03:28.793 OC练习[1832:156645] liuxingyu
2015-07-22 21:03:28.793 OC练习[1832:156645] 21
2015-07-22 21:03:28.793 OC练习[1832:156645] shangshuai
2015-07-22 21:03:28.793 OC练习[1832:156645] 21
*/