__NSCFConstantString && __NSPlaceholderDictionary

 -[__NSCFConstantString size]: unrecognized selector sent to instance 0x53ea70 

该错误是在我将NSString类型的参数赋值给UIImage类型的时候报出的,查了一会才查出是这的问题。

如果大家不是这个问题查一下是不是也是赋值类型错了。

 

[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from object

Objective-C中,NSDictionary初始化的方法有很多种

方法1:  [NSDictionary dictionaryWithObjectsAndKeys:<#(id), ...#>, nil]

方法2:  NSDictionary *dic = @{@"key":value}

方法2,如果你的value是为nil 必将引发崩溃:

'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]

意思就是说使用[__NSPlaceholderDictionary initWithObjects:forKeys:count:]这个初始化方法,发现keys count和objcects的个数不匹配了

如何规避?

在使用@{@”key”:value} 这种方式初始化的时候,一定要对value做是否为nil的判断,为nil就不要加入Dictionary

#define  UNNULL_STRING(string)      (string && ![string isKindOfClass:[NSNull class]]) ? string : @""//非空字符串

self.string = NONULL_STRING(string);

使用标准的初始化方法:

NSDictionary dictionaryWithObjectsAndKeys:value1,@"v1",value2,@"v2",value3,@"v3", nil];

或其它的几个初始化方法进行初始化,这样如果value为nil就不会加入字典,使用 objectForKey:取出来的对象就会为nil对象,不会引发崩溃。

关联:

使用@[]方法初始化NSArray也有此坑,规避方法同字典一样

 

 参考:

https://blog.csdn.net/yuanpeng1014/article/details/62215115

posted @ 2016-02-01 17:08  尘恍若梦  阅读(461)  评论(0编辑  收藏  举报