1 /*=============================NSDictionary(不可变字典)的使用=========================*/ 2 3 //————————————————————————————————字典的创建———————————————————————————————— 4 5 // 前面的是值(value),后面的是键(key),一一对应 6 // 方式一: 7 NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value",@"key",@"value1",@"key1", nil]; 8 NSLog(@"dic :%@",dic); 9 10 // 方式二: 11 NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key",@"value1",@"key1", nil]; 12 13 // 字典的value可以说任何对象类型 14 NSArray *array = @[@"11",@"22",@"33"]; 15 NSDictionary *dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:array,@"key" ,nil]; 16 NSLog(@"dic2 :%@",dic2); 17 18 NSDictionary *dic3 = [[NSDictionary alloc] initWithObjectsAndKeys:dic2,@"dicKey", nil]; 19 NSLog(@"dic3 :%@",dic3); 20 21 // 方式三:跟方式一区分,只能是不可变字典 22 NSDictionary *dic4 = @{@"key":@"vlaue",@"key1":@"value1"}; 23 NSLog(@"dic4 :%@",dic4); 24 25 26 //————————————————————————————————字典的常用方法————————————————————————————— 27 28 // count 字典元素的个数,键值一起为一个元素 29 NSUInteger len = [dic4 count]; 30 NSLog(@"len = %ld",len); 31 32 //objectForKey :根据key取对应的值,如果可以没有不会报错,返回的是null 33 id obj = [dic3 objectForKey:@"dicKey"]; 34 NSLog(@"obj:%@",obj); 35 36 //allKeys 获取字典的所有key,返回的是数组 37 NSArray *allKeys = [dic4 allKeys]; 38 NSLog(@"allKeys :%@",allKeys); 39 40 //allValues 获取字典的所有Value,返回的是数组 41 NSArray *allValues = [dic4 allValues]; 42 NSLog(@"allValues: %@",allValues); 43 44 //allKeysForObject 获取相同value的所有key 45 NSDictionary *dic5 = @{@"key":@"value",@"key1":@"value"}; 46 NSArray *allKey = [dic5 allKeysForObject:@"value"]; 47 NSLog(@"allKey :%@",allKey); 48 49 50 /*=============================NSMutableDictionary(可变字典)的使用=========================*/ 51 52 //————————————————————————————————可变字典的创建———————————————————————————————— 53 54 // 两种方式: 55 NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"abc",@"key",@"hhhh",@"key1", nil]; 56 57 NSMutableDictionary *mDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value",@"key",@"value1",@"key1", nil]; 58 59 //NSMutableDictionary *mDic2 = @{@"key":@"value",@"key1":@"value1"}; 错误:不能这样创建可变字典 60 NSLog(@"mDic1:%@",mDic1); 61 62 63 //————————————————————————————————可变字典的常用方法————————————————————————————— 64 65 //setObject:forKey 给字典添加元素,修改元素 66 [mDic1 setObject:@"value2" forKey:@"key2"]; 67 NSLog(@"mDic1:%@",mDic1); 68 // 修改key 对应的元素 69 [mDic1 setObject:@"kkkkk" forKey:@"key2"]; 70 71 //removeObjectForKey 移除指定键的元素 72 [mDic1 removeObjectForKey:@"key"]; 73 NSLog(@"mDic1:%@",mDic1); 74 75 // removeAllObjects 移除字典所有的元素 76 [mDic1 removeAllObjects]; 77 NSLog(@"mDic1:%@",mDic1); 78 79 // removeObjectsForKeys 移除一组键对应的元素 80 NSArray *keys = [mDic1 allKeys]; 81 [mDic1 removeObjectsForKeys:keys]; 82 NSLog(@"mDic1:%@",mDic1); 83 84 //setDictionary 将新字典替换旧字典 85 [mDic1 setDictionary:mDic]; 86 NSLog(@"mDic1 :%@",mDic); 87 88 89 /*===========================================字典的遍历===================================*/ 90 91 NSMutableDictionary *mDic3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"abc",@"key",@"etrtr",@"key1", nil]; 92 93 // 1、普通遍历 94 NSArray *allkey = [mDic3 allKeys]; 95 for (int i = 0; i < mDic3.count; i++) { 96 97 NSString *key = [allkey objectAtIndex:i]; 98 NSString *value = [mDic3 objectForKey:key]; 99 NSLog(@"---%@",value); 100 } 101 102 // 2、快速遍历 obj 是字典的key 103 for (id obj in mDic3) { 104 105 NSString *value = [mDic3 objectForKey:obj]; 106 NSLog(@"==%@",value); 107 } 108 109 // 3、block 遍历 110 [mDic3 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 111 112 NSLog(@">>%@",key); 113 NSLog(@">>%@",obj); 114 }];