#pragma mark ------------- NSDictionary ---------
#pragma mark 创建字典
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"mm" forKey:@"girlFriend"];//??存储中文字符
NSLog(@"dic1 : %@",dic1);
#pragma mark 创建多个字典
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"wsl",@"name",
@"m",@"gender",
@"22",@"age",
nil];
NSLog(@"dic2 = %@",dic2);
#pragma mark 根据现有字典创建字典
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
NSLog(@"dic3 = %@",dic3);
#pragma mark 根据 key值 获取 value
NSLog(@"name value: %@",[dic3 objectForKey:@"name"]);
#pragma mark 获取字典数量
NSLog(@"dic3 count: %lu",dic3.count);
#pragma mark 所有的键集合
NSArray *keys = [dic3 allKeys];
NSLog(@"allKeys : %@",keys);
#pragma mark 所有的值集合
NSArray *values = [dic3 allValues];
NSLog(@"allValues : %@",values);
#pragma mark ----------NSMutableDictionary------------
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic3];
#pragma mark 添加现有字典数据
[mutableDic addEntriesFromDictionary:dic1];
NSLog(@"mutableDic : %@",mutableDic);
#pragma mark 添加新的键值对象
[mutableDic setObject:@"18369906788" forKey:@"Phone"];
NSLog(@"mutableDic : %@",mutableDic);
#pragma mark 以新的字典数据覆盖旧的字典数据
// [mutableDic setDictionary:dic2];
// NSLog(@"mutableDic : %@", mutableDic);
#pragma mark 根据key值删除value
[mutableDic removeObjectForKey:@"age"];
NSLog(@"mutableDic : %@",mutableDic);
#pragma mark 根据key数组删除元素
NSArray *array = @[@"name",@"Phone"];
[mutableDic removeObjectsForKeys:array];
NSLog(@"removeObjectForKeys : %@",mutableDic);
#pragma mark 删除所有元素
[mutableDic removeAllObjects];
NSLog(@"remove all : %@",mutableDic);
#pragma mark 快速遍历
for (id key in mutableDic) {
NSLog(@"key : %@ value : %@",key,[mutableDic objectForKey:key]);
}
#pragma mark 枚举遍历
NSEnumerator *enumerator = [mutableDic keyEnumerator];
id key = [enumerator nextObject];
while (key) {
NSLog(@"enumerator : %@",[mutableDic objectForKey:key]);
key = [enumerator nextObject];
}
NSDictionary 字典
最新推荐文章于 2017-05-12 11:27:47 发布