[转]NSDictionary的setValue:forKey与setObject:forKey、objectForKey与valueForKey的差异

setValue:forKey与setObject:forKey的差异
在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有。

1,先看看setValue: forKey:的定义

1
2
3
4
5
@interface NSMutableDictionary(NSKeyValueCoding)
/* Send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObject:forKey:.
*/
- (void)setValue:(id)value forKey:(NSString *)key;
@end

扩展NSMutableDictionary的一个类别,上面注释说的很清楚,发送setObject:forKey 给接收者,也就是调用setObject:forKey方法
除非value为nil的时候,调用方法removeObject:forKey

2,看看setObject:forKey:的定义

1
2
3
4
@interface NSMutableDictionary :NSDictionary
- (void)removeObjectForKey:(id)aKey;
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
@end

注意:setObject:forKey:中Key的对象是一个id类型,并不是NSString,只不过我们经常使用NSString而已。

现在总结他们2者的区别就是:
1, setObject:forkey:中value是不能够为nil的,不然会报错。
setValue:forKey:中value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法
2, setValue:forKey:中key的参数只能够是*NSString类型,而setObject:forKey:的可以是任何类型

注意:setObject:forKey:对象不能存放nil要与下面的这种情况区分:
1, [imageDictionarysetObject:[NSNullnull] forKey:indexNumber];
[NSNull null]表示的是一个空对象,并不是nil,注意这点

2, setObject:forKey:中Key是NSNumber对象的时候,如下:
[imageDictionarysetObject:obj forKey:[NSNumber numberWithInt:10]];

注意:
上面说的区别是针对调用者是dictionary而言的。
setObject:forKey:方法NSMutabledictionary特有的,而
setValue:forKey:方法是KVC(键-值编码)的主要方法。

当 setValue:forKey:方法调用者是对象的时候:
setValue:forKey:方法是在NSObject对象中创建的,也就是说所有的oc对象都有这个方法,所以可以用于任何类。
比如使用:

1
2
SomeClass *someObj = [[SomeClass alloc] init];
[someObj setValue:self forKey:@"delegate"];

表示的意思是:对象someObj设置他的delegate属性的值为当前类,当然调用此方法的对象必须要有delegate属性才能设置,不然调用了也没效果

objectForKey与valueForKey的差异
从 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: 有额外一点:

1
2
3
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:]。

比如:

1
2
3
4
5
6
7
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"theValue" 

forKey:@"theKey"];

NSString *value1 = [dict objectForKey:@"theKey"];

NSString *value2 = [dict valueForKey:@"theKey"];

这时候 value1 和 value2 是一样的结果。如果是这样一个 dict:

1
2
3
4
5
6
7
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 同名字符串来获取对应的值。比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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 异常。

回过头来看刚才 crash 的例子, [dict valueForKey:@"@theKey"]; 会把 key 里的 @ 去掉,也就变成了 [dict valueForKey:@"theKey"];,而 dict 不存在 theKey 这样的 property,转而执行 [dict valueForUndefinedKey:@"theKey"];,抛出 NSUndefinedKeyException 异常后 crash 掉。

objectForKey: 和 valueForKey: 在多数情况下都是一样的结果返回,但是如果 key 是以 @ 开头,valueForKey: 就成了一个大坑,建议在 NSDictionary 下只用 objectForKey: 来取值。