不可变字典
【初始化】
1. value — key
NSDictionary * dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
2.批量设置键值对 value — key
NSArray * arr1 = @[@"1",@"2"];
NSArray * arr2 = @[@"one",@"two"];
NSDictionary * dict1 = [[NSDictionary alloc]initWithObjects:arr1 forKeys:arr2];
3. key : value
NSDictionary * dict3 = @{@"1":@"one",@"2":@"two",@"3":@"one"};
4.通过key拿到value
NSString * str1 = [dict3 objectForKey:@"1"];
5.拿到所有的key
NSArray * arr3 = [dict3 allKeys];
6.拿到所有的Value
arr3 = [dict3 allValues];
7.拿到所有相同value的key (key一定唯一存在 value不唯一)
arr3 = [dict3 allKeysForObject:@"one"];
【遍历】
1.普通遍历
for (int i = 0; i < [arr3 count]; i ++) {
NSLog(@"%@",[dict3 objectForKey:arr3[i]]);
}
2.快速遍历
for (NSString * str in dict3) {
//str 是key
NSLog(@"%@",dict3[str]);
}
【可变字典】
【初始化】
1.带参数的初始化
NSMutableDictionary * mDict = [NSMutableDictionary dictionaryWithDictionary:dict3];
2.设置键值对 value ———— key
[mDict setValue:@“one” forKey:@“1”];
3.删除键值对
[mDict removeObjectForKey:@"one"];
4.删除所有键值对
[mDict removeAllObjects];
5.根据key值 批量删除键值对
[mDict removeObjectsForKeys:arr3];
6.重置
[mDict setDictionary:dict3];