valueForKey objectForKey的差异
2016-03-29 17:09
495 查看
从 NSDictionary 取值的时候有两个方法,
先从 NSDictionary 文档中来看这两个方法的定义:
objectForKey:
returns the value associated with aKey, or nil if no value is associated with aKey.
返回指定 key 的 value,若没有这个 key 返回 nil.
valueForKey:
returns the value associated with a given key.
同样是返回指定 key 的 value。
直观上看这两个方法好像没有什么区别,但文档里 valueForKey: 有额外一点:
If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key. via Discussion
一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等同于 objectForKey:,如果是以 @ 开头,去掉 key 里的 @ 然后用剩下部分作为 key 执行 [super valueForKey:]。
比如:
这时候
这是因为
回过头来看刚才 crash 的例子,
参考:https://fann.im/blog/2012/04/12/difference-between-objectforkey-and-valueforkey-in-nsdictionary/
objectForKey:和
valueForKey:,这两个方法具体有什么不同呢?
先从 NSDictionary 文档中来看这两个方法的定义:
objectForKey:
returns the value associated with aKey, or nil if no value is associated with aKey.
返回指定 key 的 value,若没有这个 key 返回 nil.
valueForKey:
returns the value associated with a given key.
同样是返回指定 key 的 value。
直观上看这两个方法好像没有什么区别,但文档里 valueForKey: 有额外一点:
If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key. via Discussion
一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等同于 objectForKey:,如果是以 @ 开头,去掉 key 里的 @ 然后用剩下部分作为 key 执行 [super valueForKey:]。
比如:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue" forKey:@"theKey"]; NSString *value1 = [dict objectForKey:@"theKey"]; NSString *value2 = [dict valueForKey:@"theKey"];
这时候
value1和
value2是一样的结果。如果是这样一个
dict:
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue" forKey:@"@theKey"];// 注意这个 key 是以 @ 开头 NSString *value1 = [dict objectForKey:@"@theKey"]; NSString *value2 = [dict valueForKey:@"@theKey"];
value1可以正确取值,但是
value2取值会直接 crash ,报错信息:
Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<__NSCFDictionary 0x892fd80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key theKey.’
这是因为
valueForKey:是
KVC(NSKeyValueCoding)的方法,在
KVC里可以通过
property同名字符串来获取对应的值。比如:
@interface Person : NSObject @property (nonatomic, retain) NSString *name; @end ... Person *person = [[Person alloc] init]; person.name = @"fannheyward"; NSLog(@"name:%@", [person name]); //name:fannheyward NSLog(@"name:%@", [person valueForKey:@"name"]); //name:fannheyward [person release];
valueForKey:取值是找和指定
key同名的
property accessor,没有的时候执行
valueForUndefinedKey:,而
valueForUndefinedKey: 的默认实现是抛出
NSUndefinedKeyException异常。参考
Getting Attribute Values Using Key-Value Coding
回过头来看刚才 crash 的例子,
[dict valueForKey:@"@theKey"];会把 key 里的 @ 去掉,也就变成了
[dict valueForKey:@”theKey”];,而 dict 不存在 theKey 这样的 property,转而执行
[dict valueForUndefinedKey:@”theKey”];,抛出 NSUndefinedKeyException` 异常后 crash 掉。
objectForKey:和
valueForKey:在多数情况下都是一样的结果返回,但是如果 key 是以 @ 开头,
valueForKey:就成了一个大坑,建议在 NSDictionary 下只用
objectForKey:来取值。
参考:https://fann.im/blog/2012/04/12/difference-between-objectforkey-and-valueforkey-in-nsdictionary/
相关文章推荐
- Objective-C自定义加载菊花UIActivityIndicatorView
- java.lang.Object.clone()分析
- map和object互相转换
- java源码分析 ---- Object类
- Python报错TypeError: 'str' object is not callable
- cocoapod 引入头文件自动补齐
- 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- iOS高效开发必备的10款Objective-C类库
- Java的Map与Object互转
- iOS开发66-setValue:forKey:和 setObject:forKey:的区别
- Objective-C中@property和@synthesize的用法
- Objective-C编码规范[译]
- js中数组(array)和对象(object)的区别
- 从C语言的变量声明到Objective-C中的Block语法
- object references an unsaved transient instance - save the transient instance before flushing错误
- Volley使用JsonObjectRequest发送Post请求失败
- delphi 组件容器TObjectList代替List
- Objective-C中的runtime详解_1
- swift学习-AnyObject类型记录
- Xcode的一些常用配置