37-oc NSDictionary
Posted wc&Home
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了37-oc NSDictionary相关的知识,希望对你有一定的参考价值。
NSDictionary
什么是字典
OC中的NSDictionary:根据key找到value,字典中存储的东西都是键值对
如何创建字典
方法一:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lj" forKey:@"name"];
//根据key获取value
NSString *name = [dict objectForKey:@"name"];
NSLog(@"name = %@", name);
方法二:
// 注意: key和value 是一一对应
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lj", @"30", @"1.75"] forKeys:@[@"name", @"age", @"height"]];
NSLog(@"%@ %@ %@", [dict objectForKey:@"name"], [dict objectForKey:@"age"], [dict objectForKey:@"height"]);
方法三:"简写:
NSDictionary *dict = @{key:value};
NSDictionary *dict = @{@"name": @"lj"};
NSLog(@"%@", dict[@"name"]);
NSDictionary *dict = @{@"name":@"lj", @"age":@"30", @"height":@"1.75"};
NSLog(@"%@ %@ %@", dict[@"name"], dict[@"age"], dict[@"height"]);
如何对字典进行遍历
NSDictionary *dict = @{@"name":@"lj", @"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);
}];
字典文件进行读写
将字典数据写入文件中
NSDictionary *dict = @{@"name":@"lj", @"age":@"30", @"height":@"1.75"};
[dict writeToFile:@"/Users/xiaomage/Desktop/info.plist" atomically:YES];
从文件中读取字典数据
注意: 字典和数组不同, 字典中保存的数据是无序的
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];
NSMutableDictionary
如何对可变字典进行增删改查
如何创建一个空的可变字典
NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
如何给可变字典添加键值对
[dictM setObject:@"lj" forKey:@"name"];
//简写:
dictM[@"name"] = @"lj";
// 会将传入字典中所有的键值对取出来添加到dictM中
[dictM setValuesForKeysWithDictionary:@{@"age":@"30", @"height":@"1.75"}];
如何根据键,获取可变字典的值
NSLog(@"name = %@", dictM[@"name"]);
如何删除可变字典键值对
[dictM removeObjectForKey:@"name"];
[dictM removeObjectsForKeys:@[@"age", @"height"]];
如何修改可变字典中的键值对
利用setObject方法给同名的key赋值, 那么新值会覆盖旧值
[dictM setObject:@"88" forKey:@"age"];
//简写:
dictM[@"age"] = @"88";
使用可变字典有什么注意事项
1.不能使用@{}来创建一个可变的字典
NSMutableDictionary *dictM = @{@"name":@"lj"};//编译就会报错
[dictM setObject:@"30" forKey:@"age"];
2.
如果是不可变字典, 那么key不能相同
如果是不可变字典出现了同名的key, 那么后面的key对应的值不会被保存
如果是在可变数组中, 后面的会覆盖前面的
NSDictionary *dict = @{@"name":@"lm", @"name":@"lj"};
NSLog(@"dict = %@", dict);
NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithObjects:@[@"lmj", @"lj"] forKeys:@[@"name", @"name"]];
NSLog(@"dict = %@", dictM);
NSArray和NSDictionary的区别
NSArray是有序的,NSDictionary是无序的
NSArray是通过下标访问元素,NSDictionary是通过key访问元素
以上是关于37-oc NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章