#pragma mark 不可变字典
// 创建 字典
快速创建:@{key1:value1,key2:value2};
快速访问字典元素:字典名[key]
// 静态创建
NSDictionary * dic1 = [NSDictionarydictionaryWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2", nil];
// 实例创建
NSDictionary * dic2 = [[NSDictionaryalloc]initWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2", nil];
// 来自其他字典
NSDictionary * dic3 = [NSDictionarydictionaryWithDictionary:dic1];
// 获取字典中元素的个数
NSInteger count = [dic1 count];
// 获取字典的value
NSString * dicStr = [dic1 objectForKey:@"key1"];
// 获取字典的键和值
NSArray * keys = [dic1 allKeys];
NSArray * value = [dic1 allValues];
NSLog(@"**********%@",keys);
#pragma mark 可变字典
// 静态创建
NSMutableDictionary * muDic0 = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2", nil];
NSMutableDictionary * muDic1 = [NSMutableDictionarydictionaryWithCapacity:0];
// 实例创建
NSMutableDictionary * muDic2 = [[NSMutableDictionaryalloc]initWithCapacity:0];
// 向字典添加元素
[muDic2 setValue:@"Value"forKey:@"Key"];
// 删除指定的元素
[muDic0 removeObjectForKey:@"key1"];
// 向字典muDic2添加整个字典muDic0
[muDic2 addEntriesFromDictionary:muDic0];
// 根据指定的数组删除字典的元素
NSArray * array = [NSArrayarrayWithObjects:@"key1", nil];
[muDic0 removeObjectForKey:array];
// 将字典muDic2的内容设置与字典muDic0相同
[muDic2 setDictionary:muDic0];
// 删除所有元素
[muDic0 removeAllObjects];
// 遍历字典
for (id key in muDic0) {
id str = [muDic0 objectForKey:key];
}
NSDictionary * dictionary = @{@"name":@"jack",@"qq":@"1234",@"age":@"11"};
NSArray *index = dictionary.allKeys;
for (int i = 0; i < dictionary.count; i ++) {
NSString * key = index[i];
NSString * str = [dictionary objectForKey:key];
NSLog(@"%@ :%@",key,str);
}
// block 快速遍历
[dictionary enumerateKeysAndObjectsUsingBlock:^(id_Nonnull key, id_Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"键:%@,值:%@",key,obj);
}];