setValue和setObject的区别

来源:互联网 发布:php入门很简单 pdf 编辑:程序博客网 时间:2024/05/02 06:11
1 setValue: forKey:的定义
@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
value 为 nil ,调用 removeObject:forKey:
value不为nil时调用 setObject:forKey:
key为NSString类型。
2 setObject:forKey:的定义
@interface NSMutableDictionary : NSDictionary
- (void)removeObjectForKey:(id)aKey;
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
@end


anobject不能为nil,而且key是一个id类型,不仅限于NSString类型


两者的区别:
(1)setObject:forkey:中value是不能够为nil的;setValue:forKey:中value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法
(2)setValue:forKey:中key只能够是NSString类型,而setObject:forKey:的可以是任何类型
原创粉丝点击