在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有。
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
扩展NSMutableDictionary的一个类别,上面注释说的很清楚,发送setObject:forKey 给接收者,也就是调用setObject:forKey方法
除非value为nil的时候,调用方法removeObject:forKey

2.看看setObject:forKey:的定义
@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对象都有这个方法,所以可以用于任何类。
比如使用:
SomeClass *someObj = [[SomeClass alloc] init];
[someObj setValue:self forKey:@"delegate"];
表示的意思是:对象someObj设置他的delegate属性的值为当前类,当然调用此方法的对象必须要有delegate属性才能设置,不然调用了也没效果

setObject与setValue的区别的更多相关文章

  1. IOS之KVC和KVO(未完待续)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. NSDictionary , NSMutableDictionary, NSMutableDictionary 和 NSMutableSet)相当于java的map、set

    1 NSDictionary 和 NSMutableDictionary NSDictionary  :就是java中的map; 放入对象是键值对 key-value  , 同样 秉持了一样的原则,只 ...

  3. setValue和setObject的区别

    在NSMutableDictionary的方法中有setValue forKey与setObject forKey,它们都可以用来设置某一个key值对应的value 1,setValue: forKe ...

  4. [转]setValue和setObject的区别

    在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有. 1,先看看setV ...

  5. setValue 和 setObject 的区别

    在使用NSMutableDictionary 的时候经常会使用setValue forKey 与 setObject forKey,他们经常是可以交互使用的. 1.setValue forKey的定义 ...

  6. IOS [转]setValue和setObject的区别

    在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有. 1,先看看setV ...

  7. NSMutableDictionary中 setValue和setObject的区别

    对于- (void)setValue:(id)value forKey:(NSString *)key;函数 官方解释如下 Send -setObject:forKey: to the receive ...

  8. Objective-c setObject:forKey:和setValue:forKey:的区别

    setObject:forKey: 是NSMutableDictionary类的方法                               key参数类型可以是任意类型对象           ...

  9. setObject:forKey和setValue:forKey的区别

    setObject:forKey: 是NSMutableDictionary类的方法                              key参数类型可以是任意类型对象             ...

随机推荐

  1. hightchart or hightstock 格式Y数据

    hightchart or hightstock 格式Y数据,鼠标放在上面显示两位小数 方法一: tooltip: { shared: true, crosshairs: true , formatt ...

  2. Storm Esper

    https://github.com/hellojinjie https://github.com/tomdz/storm-esper https://github.com/jayway/awaiti ...

  3. index seek与index scan

    原文地址:http://blog.csdn.net/pumaadamsjack/article/details/6597357 低效Index Scan(索引扫描):就全扫描索引(包括根页,中间页和叶 ...

  4. 【Java之】多线程学习笔记

    最近在学习thinking in java(第三版),本文是多线程这一章的学习总结. --------------------------------------------------------- ...

  5. 又一道简单题&&Ladygod(两道思维水题)

    Ladygod Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  6. linux系统关机与重新启动命令

    在linux下关机和重新启动系统有shutdown.halt.reboot.init,对于他们来说他们的内部工作过程是不同样的. 1.shutdown命令 使用它能够安全地关闭系统.然而在关闭系统时. ...

  7. struts2 s:textfield

    初学struts2,在头脑中一直在想一个问题,就是对于struts2 ,当应用其自身的标签时,例如: <s:form> <s:textfield name="a" ...

  8. C# 利用file打印日志

    public class FaceLog { public static void AppendInfoLog(string errMsg) { try { string Folder = Main. ...

  9. c# foreach枚举器

    要是自己的类支持foreach ,必须在类中必须有GetEnumerator方法,该方法返回的是一个IEnumerator类型的枚举器; public class MyStruct { public ...

  10. pagination jquery最简单的分页【无刷新和刷新都通用】

    参数说明 参数名 描述 参数值 maxentries 总条目数                           必选参数,整数 items_per_page 每页显示的条目数            ...