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

setObject:forKey: 是NSMutableDictionary类的方法

key参数类型可以是任意类型对象

value不能为nil,不然会报错

setValue:forKey: 是NSObject类的方法也就是说所有的OC对象都有这个方法

key值必须是字符串

value可以为nil,但是当value为空时,会自动调用removeObject:forKey方法

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

[imageDictionarysetObject:[NSNullnull] forKey:indexNumber];

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

2、setObject:forKey:中Key是NSNumber对象的时候,如下:

[imageDictionarysetObject:obj forKey:[NSNumber numberWithInt:10]];

时间: 2024-08-25 17:25:33

Objective-c setObject:forKey:和setValue:forKey:的区别的相关文章

setObject:forKey:与setValue:forKey:的区别

首先不可变字典可以调起setValue:forKey:,但不能真正的进行操作,这取决与不可变字典不可增删改的特性. 然后看一下setObject:forKey: . setValue:forKey: . setValue:forKeyPath: 的标准调用语句: [muDict setValue:<#(nullable id)#> forKey:<#(nonnull NSString *)#>];[muDict setValue:<#(nullable id)#> fo

setObject:forKey和setValue:forKey的区别

setObject:forKey: 是NSMutableDictionary类的方法                              key参数类型可以是任意类型对象                              value不能为nil,不然会报错 setValue:forKey: 是NSObject类的方法也就是说所有的OC对象都有这个方法                            key值必须是字符串                            v

setValue: forKey: 和 setValue: forKeyPath:

一. 先说下setObject forKey 和 setValue forKey 1.setObject:ForKey: 是NSMutableDictionary特有的:setValue:ForKey:是KVC的主要方法:2.setObject:ForKey:中object对象不能为nil,不然会报错:key的参数只要是对象就可以,并局限于 NSString:                                                                     

setValue:forKey:和setObject:forKey:调用者是dictionary时的区别

通常使用NSMutableDictionary时经常会使用setValue:forKey:和setObject:forKey:他们一般情况下是可以交互使用的,但是他们在特定情况下是有区别的: 官方文档中有如下注释: /* Send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObjectForKey:. */ - (void)setValue:(nullable Ob

setValue:forKey:和 setObjectforKey: 区别

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;@endvalue 为 nil ,

iOS.KVC.setValue:forKey:

Foundation Framework 定义了 NSObject(NSKeyValueCoding), - (void)setValue:(id)value forKey:(NSString *)key; 该接口的说明 1. 先在message receiver的class中搜索方法 -set<Key>: 2. 如果1中未找到,则在class中以此搜索 _<key>, _is<key>, <key>, is<Key> 实例变量 3. 如果2中未

IOS setValue forKey

NSObjiect *obj:[obj setValue:value forKey:@"cpname"]复制代码的时候都会出现这个异常this class is not key value coding-compliant for the key cpname; 原因:obj不包含属性cpname 可以考虑使用 NSMutaleDictionary(Mutable) 类型的变量保存

NSMutableDictionary 类中增加键值对方法分析

在iOS中可变字典增加一个键值对的方法有setObject: forKey: 和setValue : forKey: .为了方便我们把这两个方法简称为方法A和方法B. B这个方法中其中的value值是不能为nil,否则程序会出项崩溃.而A方法中的这个value可以为nil,但是当这个value位nil时,系统会自动调用removeObjectforKey:这个方法.这样就把这个键值对删除掉了.B方法中的key值可以是任意类型的,但是这个类型必须要实现NSCopying协议.而A方法中它的key值

OC ---- 字典集合 iOS学习-----细碎知识点总结

实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:@"悟空", @"name", @"男", @"gender", @"500", @"age", nil ]; NSLog(@"%@", wukong); // 便利构造器创建 NSDictionary *wuNeng