本文主要是介绍setValue和setObject的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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:的可以是任何类型
这篇关于setValue和setObject的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!