OC5(字典,集合)
字典中key是不能重复使用的,但是value可以重复,并且字典是一个无序集合
1,不可变字典
1,初始化方法
NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"dada",@"name1",@"zhuzhu",@"name2",@"dazi",@"name3", nil];
便利构造器
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhonger",@"name1",@"taixu",@"name2",@"zuizui",@"name3", nil];(先给value再给key)
笑笑语法
NSDictionary *dic3 = @{@"1":@"zuizui",@"2":@"fufu",@"3":@"ganggang"};键值对之间用,隔开
2,获取字典中键值对的个数以及所有的key和所有的value(count、allKeys、allValues)
3,通过key获取字典中某个Value值
[dic1 objectForKey:@“name1”];
2,可变字典
1,创建可变字典
NSMutableDictionary *mDic = [[NSMutableDictionary alloc]initWithCapacity:0];
NSMutableDictionary *mDic1 = [NSMutableDictionary dictionaryWithCapacity:0];
NSMutableDictionary *mDic2 = [@{@"key1":@"zhangsan",@"key2":@"wangwu"} mutableCopy];
2,增加(或者修改)键值对的方法
[mDic2 setObject:@"lisi" forKey:@"key3"];
3,删除键值对的方法
[mDic2 removeObjectForKey:@"key1"];
4,移除所有的键值对
[mDic2 removeAllObjects];