NSDictionary及NSMutableDictionary
NSDictionary
1:什么是字典?
OC中的NSDictionary:根据key找到value,字典中存储的东西都是键值对
2:如何创建字典?
方法一:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"]; //根据key获取value NSString *name = [dict objectForKey:@"name"]; NSLog(@"name = %@", name);
方法二:
/
/ 注意: key和value 是一一对应 NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lnj", @"30", @"1.75"] forKeys:@[@"name", @"age", @"height"]]; NSLog(@"%@ %@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"age"], [dict objectForKey:@"height"]); 方法三:"简写: NSDictionary *dict = @{key:value}; NSDictionary *dict = @{@"name": @"lnj"}; NSLog(@"%@", dict[@"name"]); NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; NSLog(@"%@ %@ %@", dict[@"name"], dict[@"age"], dict[@"height"]);
3:如何对字典进行遍历?
NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; //获取字典中key和value的个数, 在字典中key称之为键, value称之为值 NSLog(@"count = %lu", [dict count]);
方法一:老式for循环写法
for (int i = 0; i < dict.count; ++i) { // 获取字典中所有的key NSArray *keys = [dict allKeys]; // 取出当前位置对应的key // NSLog(@"%@", keys[i]); NSString *key = keys[i]; NSString *value = dict[key]; NSLog(@"key = %@, value = %@", key, value); }
方法二:增强for循环写法
// 如何通过forin遍历字典, 会将所有的key赋值给前面的obj for (NSString *key in dict) { // NSLog(@"%@", key); NSString *value = dict[key]; NSLog(@"key = %@, value = %@", key, value); }
方法三:OC字典的迭代器来遍历
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"key = %@, value = %@", key, obj); }];
4:如何对字典文件进行读写?
1.将字典数据写入文件中
NSDictionary *dict = @{@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; [dict writeToFile:@"/Users/xiaomage/Desktop/info.plist" atomically:YES];
2.从文件中读取字典数据
- 注意: 字典和数组不同, 字典中保存的数据是无序的
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/xiaomage/Desktop/info.plist"]; NSLog(@"%@", newDict); NSArray *arr = @[@10, @20, @30, @5]; [arr writeToFile:@"/Users/xiaomage/Desktop/abc.plist" atomically:YES];
9.NSMutableDictionary
如何对可变字典进行增删改查
1:如何创建一个空的可变字典?
NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
2:如何给可变字典添加键值对?
[dictM setObject:@"lnj" forKey:@"name"]; //简写: dictM[@"name"] = @"lnj"; // 会将传入字典中所有的键值对取出来添加到dictM中 [dictM setValuesForKeysWithDictionary:@{@"age":@"30", @"height":@"1.75"}];
3:如何根据键,获取可变字典的值?
NSLog(@"name = %@", dictM[@"name"]);
4:如何删除可变字典键值对?
[dictM removeObjectForKey:@"name"]; [dictM removeObjectsForKeys:@[@"age", @"height"]];
5:如何修改可变字典中的键值对?
- 利用setObject方法给同名的key赋值, 那么新值会覆盖旧值
[dictM setObject:@"88" forKey:@"age"]; //简写: dictM[@"age"] = @"88";
6:使用可变字典有什么注意事项?
1.不能使用@{}来创建一个可变的字典
NSMutableDictionary *dictM = @{@"name":@"lnj"};//编译就会报错 [dictM setObject:@"30" forKey:@"age"];
2.
- 如果是不可变字典, 那么key不能相同
- 如果是不可变字典出现了同名的key, 那么后面的key对应的值不会被保存
- 如果是在可变数组中, 后面的会覆盖前面的
NSDictionary *dict = @{@"name":@"lmj", @"name":@"lnj"}; NSLog(@"dict = %@", dict); NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithObjects:@[@"lmj", @"lnj"] forKeys:@[@"name", @"name"]]; NSLog(@"dict = %@", dictM);
未来有你才精彩,你有未来不是梦!