字典 setValue和 setObject的不同

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

1, setObject:forkey:中value是不能够为nil的,不然会报错。

setValue:forKey:中value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法

2, setValue:forKey:中key的参数只能够是NSString类型,而setObject:forKey:的可以是任何类型

注意:setObject:forKey:对象不能存放nil要与下面的这种情况区分:

NSMutableDictionary* dic [email protected]{@"k1":@"好人",@"k2":@"坏人",@"k3":@"男人",@"k4":@"女人"};

1, [dic  setObject:[NSNullnull] forKey:@"k5"];

[NSNull null]表示的是一个空对象,并不是nil,注意这点

时间: 2024-08-08 01:28:43

字典 setValue和 setObject的不同的相关文章

NSUserDefault、NSMutableDictionary的setValue和setObject区别

NSDictionary: setValue: forKey:@"" NSMutableDictionary: setObject: forKey:@"" value是不能够为nil的,不然会报错. setObject:forKey:的key可以是任何类型 setValue: forKey:@"" value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法 setValue:forKey:中key

setValue 和 setObject 的区别

在使用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 -removeObjec

setValue和setObject的区别

在使用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 s

[转]setValue和setObject的区别

在使用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 s

iOS setValue和setObject的区别

在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有.1.先看看setValue: forKey:的定义 扩展NSMutableDictionary的一个类别,上面注释说的很清楚,发送setObject:forKey 给接收者,也就是调用setObject:forKey方法,除非value为nil的时候,调用方法removeObject:forKey 2.看看setObj

setValue和setObject

1, setObject:forkey:中value是不能够为nil的,不然会报错. setValue:forKey:中value能够为nil,但是当value为nil的时候,会自动调用removeObject:forKey方法 2, setValue:forKey:中key的参数只能够是NSString类型,而setObject:forKey:的可以是任何类型 注意:setObject:forKey:对象不能存放nil要与下面的这种情况区分: 1, [imageDictionarysetObj

IOS [转]setValue和setObject的区别

在使用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 s

objective-c可变字典

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 1 #pragma mark *****************************字典********************************  2 //        字典:通过key来取值的 key值必须是成对出现的,key不能为空(nil)  3 

OC --(5)-- 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序

字典 1.快速枚举 forin   运行时不能更改内部数据 for (<#type *object#> in <#collection#>) { <#statements#> }         //对象                    //数组 for (Contact *contact in friends) {  [contact show];  } 判定是否为空  NSString *ns ==nil      NSInteger inte==0 一.字典