/*
* 不可变字典--NSDictionary
*/
//字典初始化
NSNumber *number1 = [NSNumber numberWithInt:10];
NSNumber *number2 = [NSNumber numberWithInt:12];
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:number1forKey:@"key1"];
NSLog(@"dict1 : %@", dict1);
// 初始化两个元素
NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:number1,@"Key1", number2, @"key2", nil];
NSLog(@"dict2 : %@", dict2);
// 用 NSDictionary 初始化
NSDictionary *dict3 = [NSDictionary dictionaryWithDictionary:dict2];
NSLog(@"dict3 : %@", dict3);
// 以文件来初始化 NSDictionary
//示例中路径在测试要修改为当前环境目录
NSString *path=@"/Users/hehehe/Desktop/12.plist";
NSDictionary *dict4 = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dict4 : %@", dict4);
// 字典常用的方法
// 获取字典数量
NSInteger count = [dict3 count];
NSLog(@"count : %ld", count);
// 通过key获取对应的value对象
NSObject *valueObj = [dict3 objectForKey:@"Key1"];
//NSString *valueObj = [dict3 objectForKey:@"Key1"]; //转换为具体类型
//NSInteger valueObj = [dict3 objectForKey:@"Key1"];
NSLog(@"valueObj : %@", valueObj);
//将字典的key转成一个枚举对象,用于遍历
NSEnumerator *enumerator = [dict3 keyEnumerator];
NSLog(@"enumerator : %@", enumerator);
// 获取所键的集合
NSArray *keys = [dict3 allKeys];
NSLog(@"keys : %@", keys);
// 获取所有的集合
NSArray *values = [dict3 allValues];
NSLog(@"values : %@", values);
/*
* 可变字典--NSMutableDictionary继承自NSDictionary
*/
//初始化
NSMutableDictionary *mutableDict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];
NSLog(@"mutableDict2 : %@", mutableDict2);
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"v3"forKey:@"key3"];
NSLog(@"dictionary3 : %@", dictionary3);
//向字典2对象中添加整个字典对象3
[mutableDict2 addEntriesFromDictionary:dictionary3];
NSLog(@"mutableDict2 : %@", mutableDict2);
// 向字典2对象中追加一个新key和value
[mutableDict2 setValue:@"object"forKey:@"key4"];
NSLog(@"mutableDict2 : %@", mutableDict2);
// 初始化一个空的可变字典
NSMutableDictionary *mutableDict1 = [NSMutableDictionary dictionary];
NSLog(@"mutableDict1 : %@", mutableDict1);
//将字典1对象内容设置与字典2对象相同
// 设置时会先清空 mutableDict1 对象的内容,然后在设置 mutableDict2对象中的内容
[mutableDict1 setDictionary:mutableDict2];
NSLog(@"mutableDict1 : %@", mutableDict1);
// 将字典中key1对应的值删除
[mutableDict1 removeObjectForKey:@"key1"];
NSLog(@"mutableDict1 : %@", mutableDict1);
// 根据指定的数组 {key} 移除字典的内容
NSArray *array = [NSArray arrayWithObjects:@"key4",nil];
[mutableDict1 removeObjectsForKeys:array];
NSLog(@"mutableDict1 : %@", mutableDict1);
// 删除字典所有对象
[mutableDict1 removeAllObjects];
NSLog(@"mutableDict1 : %@", mutableDict1);
/*
* 遍历字典
*/
//一般的枚举
NSArray *mutableKeys = [mutableDict2 allKeys];
NSInteger length = [mutableKeys count];
for (int i =0; i < length; i++) {
id key = [mutableKeys objectAtIndex:i];
id obj = [mutableDict2 objectForKey:key];
NSLog(@"%@", obj);
}
//快速枚举
for (id keyin mutableDict2) {
id obj = [mutableDict2 objectForKey:key];
NSLog(@"%@", obj);
}
NSLog(@"------------------------------------------------------");
// 通过枚举类型枚举
NSEnumerator *enumerators = [mutableDict2 keyEnumerator];
id key = [enumerators nextObject];
while(key) {
id obj = [mutableDict2 objectForKey:key];
NSLog(@"%@", obj);
key = [enumerators nextObject];
}