OC:OC中的集合类-NSDictionary(3)
/*
NSDictionary 字典
以NSString对象为索引的。
NSDictionary对象是一组键-值对的集合。
键通常是NSString对象,值可以使任意类型的对象。
子弹对象所保存的键-值对是无序的。
*/
//========================================================
//创建字典 空,不可变,没有用
NSDictionary *dict1 = [NSDictionary dictionary];
//创建一个存了一个元素的字典 有一对元素
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"Hello" forKey:@"word"];
//取数据
[dict2 objectForKey:@"word"];
NSLog(@"%@",[dict2 objectForKey:@"word"]);
//========================================================
//通过两个数组(值、Key)来存储信息
NSArray *ary1 = [NSArray arrayWithObjects:@"WuYuqiu",@"21",@"2014", nil];
NSArray *ary2 = @[@"name",@"age",@"year"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:ary1 forKeys:ary2];
NSLog(@"字典信息:%@",dict3);
//========================================================
//存储多个信息(一个值,一个键……)
//可读性很差,写的时候最好换行
NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"WuYuqiu",@"name",
@"21",@"age",
@"2014",@"year",nil];
NSLog(@"依次输出字典信息:%@",dict4);
//========================================================
//快速创建字典
NSDictionary *dict5 = @{@"name":@"PiKaqiu",@"age":@"22",@"no":@"2011"};
NSLog(@"快速 创建字典 dict5 %@",dict5);
//计算字典中元素个数(对)
NSLog(@"数量 %ld",dict5.count);
//取出字典中全部的key、value、某个value对应的所有key
[dict5 allKeys];
[dict5 allValues];
[dict5 allKeysForObject:@"name"];
NSLog(@"%@",[dict5 allKeys]);
NSLog(@"%@",[dict5 allValues]);
NSDictionary *dict6 = @{@"name":@"WuYuqiu",@"age":@"22",@"no":@"22"};
//值可以相同
NSLog(@"快速 创建字典 dict6 %@",dict6);
NSLog(@"某个value对应的所有key %@",[dict6 allKeysForObject:@"22"]);
//=====================================================================
/*
NSMutableDictionary 可变字典
*/
//创建一个可变的空字典 ,可以继续添加键值对
NSMutableDictionary *dict7 = [NSMutableDictionary dictionary];
//添加键值对
//字典中不能有重复的key,多次对某个key赋值时只保存最后一个值
[dict7 setObject:@"apple" forKey:@"name"];
[dict7 setObject:@"22" forKey:@"age"];
[dict7 setObject:@"20" forKey:@"age"];
[dict7 setObject:@"2011" forKey:@"year"];
[dict7 setObject:@"2012" forKey:@"no"];
NSLog(@"%@",dict7);
//删除字典中的某一组
[dict7 removeObjectForKey:@"age"];
NSLog(@"%@",dict7);
//删除多组数据
NSArray *ary4 = @[@"name",@"no"];
[dict7 removeObjectsForKeys:ary4];
NSLog(@"%@",dict7);
//删除全部数据
[dict7 removeAllObjects];
NSLog(@"%@",dict7);
//遍历字典
NSDictionary *dict8 = @{@"name":@"WuYuqiu",@"age":@"22",@"no":@"22"};
for (id obj in dict8) {
NSLog(@"遍历字典 dict8 Key:%@ Value:%@",obj,[dict8 objectForKey:obj]);
}
//变成一个数组再遍历
NSArray *ary5 = [dict8 allKeys];
for (id obj in ary5) {
NSLog(@"遍历字典 dict8 Key:%@ Value:%@",obj,[dict8 objectForKey:obj]);
}
//----------------------练习-----------------------------------------------
NSString *dict_str = [[NSString alloc] initWithContentsOfFile:@"/Users/jerehedu/Desktop/dict.txt" encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@",dict_str);
NSMutableArray *getValue_ary = [NSMutableArray array];
NSMutableArray *getKey_ary = [NSMutableArray array];
NSArray *ary = [dict_str componentsSeparatedByString:@"\n"];
for (int i = 0; i < ary.count; i++) {
if (i%2 == 0) {
[getKey_ary addObject:[ary[i] substringFromIndex:1]];//从当前位置截取到末尾,即删除#
}else{
[getValue_ary addObject:ary[i]];
}
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int j = 0; j < getKey_ary.count; j++) {
[dict setObject:getValue_ary[j] forKey:getKey_ary[j]];
}
char word[20];
NSLog(@"请输入您要翻译的单词:");
scanf("%s",word);
NSString *str1 = [NSString stringWithUTF8String:word];
BOOL flag = NO;
for (id obj in dict) {
if ([str1 isEqualTo:obj]) {
NSLog(@"翻译:%@",[dict objectForKey:obj]);
flag = YES;
}
}
if (!flag) {
NSLog(@"未找到匹配的翻译内容");