*不可变字典*
不可变字典的定义
// 初始化定义一个空的字典
NSDictionary *dic = [[NSDictionary alloc] init];
// 便利器构造器定义一个空的字典
NSDictionary *dic = [NSDictionary dictionary];
// 字面量定义一个字典
NSDictionary *dic = @{@"1" : @"2", @"3" : @"4", @"5" : @"6", @"7" : @"8"};
// 初始化定义一个字典
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"2", @"3", @"4", nil];
// key是唯一的, 但是value可以重复
// key不能为空
count 当前有多少键值对
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"2", @"3", @"4", nil];
NSLog(@"%ld", dic.count);
objectForKey: 通过对应的key找到相应的value
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"2", @"3", @"4", nil];
NSLog(@"%@", [dic objectForKey:@"2"]);
//也可以
NSLog(@"%@",dic[@"2"]);
获取字典里所有的key 和 value
NSLog(@"%@", dic.allKeys);
NSLog(@"%@", dic.allValues);
可变字典
可变字典的定义
// 空的字典
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
// 非空字典
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", @"5", @"6", nil];
forin 遍历字典
// forin 的前部分是字典里的key
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", @"5", @"6", nil];
for (NSString *str in dic) {
NSLog(@"%@", [dic objectForKey:str]);
}
setObject:
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", @"5", @"6", nil];
// 对可变的字典,当前的key不存在的时候,进行键值对的添加,当key存在了,进行值的修改
[dic setObject:@"7" forKey:@"8"];
NSLog(@"%@", dic);
[dic setObject:@"100" forKey:@"6"];
NSLog(@"%@", dic);
removeObjectForKey: 移除键值对
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", @"5", @"6", nil];
[dic removeObjectForKey:@"8"];
NSLog(@"%@", dic);
// 移除所有键值对
[dic removeAllObjects];
NSLog(@"%@", dic);
字典里嵌套数组
NSArray *arr1 = @[@"1", @"2", @"3", @"4"];
NSArray *arr2 = @[@"5", @"6", @"7", @"8"];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr1, @"arr1", arr2, @"arr2", nil];
for (NSString *str in dic) {
// 针对对应的key找到value, value是一个数组,所以用一个数组对象来接受
NSArray *arr = dic[str]; //也可以NSArray *arr = [dic objectForKey:str];
for (NSString *temp in arr) {
NSLog(@"%@", temp);
}
}
数组里嵌套字典
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"杨林", @"name", @"19", @"age", nil];
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"李牧燃", @"name", @"22", @"age", nil];
NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"腾飞", @"name", @"23", @"age", nil];
NSArray * arr = @[dic1, dic2, dic3];
for (NSMutableDictionary *dic in arr) {
for (NSString *str in dic) {
NSLog(@"%@", dic[str]);
}
}
字典里嵌套数组再嵌套字典
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"杨林", @"name", @"19", @"age", nil];
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"李牧燃", @"name", @"22", @"age", nil];
NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"腾飞", @"name", @"23", @"age", nil];
NSArray *arr1 = @[dic1, dic2];
NSArray *arr2 = @[dic3];
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:arr1, @"arr1", arr2, @"arr2", nil];
for (NSArray *arr in bigDic) {
NSArray *temp = bigDic[arr];
for (NSMutableDictionary * dic in temp) {
for (NSString *str in dic) {
NSLog(@"%@", dic[str]);
}
}
}
省市区字典数组
// 准备工作
NSString *path = @"/Users/dlios/Documents/OC_省市区字典数组/OC_省市区字典数组/area.txt";
// 根据路径产生相应的字符串
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// 对字符串逐行进行切割
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
// 创建一个省数组
NSMutableArray *proArr = [NSMutableArray array];
for (NSString *temp in strArr) {
if (![temp hasPrefix:@" "]) {
// 创建一个省字典
NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
// 给省字典添加键值对
// 省名
[proDic setObject:temp forKey:@"proName"];
// 创建一个市数组
NSMutableArray *cityArr = [NSMutableArray array];
// 添加到省字典里
[proDic setObject:cityArr forKey:@"cityArr"];
// 把省字典添加到省数组里
[proArr addObject:proDic];
} else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]){
// 创建一个市字典
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
// 给市字典添加键值对
// 市名
[cityDic setObject:temp forKey:@"cityName"];
// 创建一个区数组
NSMutableArray *zoneArr = [NSMutableArray array];
// 添加到市字典里
[cityDic setObject:zoneArr forKey:@"zoneArr"];
// 给市字典找一个位置
// 先找到对应的省字典
NSMutableDictionary *proDic = [proArr lastObject];
// 再找到对应的市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
// 将市字典添加到市数组里
[cityArr addObject:cityDic];
} else {
// 先找省字典
NSMutableDictionary *proDic = [proArr lastObject];
// 市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
// 市字典
NSMutableDictionary *cityDic = [cityArr lastObject];
// 区数组
NSMutableArray *zoneArr = cityDic[@"zoneArr"];
// 将区名添加到区数组里
[zoneArr addObject:temp];
}
}
// forin进行遍历省数组
for (NSMutableDictionary *dic in proArr) {
NSLog(@"%@", dic[@"proName"]);
NSMutableArray *city = dic[@"cityArr"];
for (NSMutableDictionary *citydic in city) {
NSLog(@"%@", citydic[@"cityName"]);
NSMutableArray *zoneArr = citydic[@"zoneArr"];
for (NSString *str in zoneArr) {
NSLog(@"%@", str);
}
}
}
例题
NSMutableDictionary *book1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"三国演义", @"name", @"100", @"price", nil];
NSMutableDictionary *book2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"水浒传", @"name", @"200", @"price", nil];
NSMutableDictionary *book3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"西游记", @"name", @"300", @"price", nil];
NSMutableArray *arrBook = [NSMutableArray arrayWithObjects:book1, book2, book3, nil];
for (NSMutableDictionary *dic in arrBook) {
// if ([dic[@"name"] isEqualToString:@"水浒传"]) {
if ([[dic objectForKey:@"name"] isEqualToString:@"水浒传"]) {
// dic[@"price"] = @"100";
[dic setObject:@"100" forKey:@"price"];
}
}
for (NSMutableDictionary *dic in arrBook) {
NSLog(@"%@, %@", dic[@"name"], dic[@"price"]);
}