Ios 尝试从对象[1]插入nil对象
在我的项目中,当我滚动得太快时,会出现一个错误,显示:Ios 尝试从对象[1]插入nil对象,ios,objective-c,nsexception,Ios,Objective C,Nsexception,在我的项目中,当我滚动得太快时,会出现一个错误,显示: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' *** First throw call stack: (0
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:
attempt to insert nil object from objects[1]'
*** First throw call stack:
(0x180a39900 0x1800a7f80 0x1809281a8 0x180928040 0x1000a3994 0x1000a2b30
0x10016e784 0x185f4c108 0x185790810 0x18578b89c 0x185727778
0x183136b2c 0x183131738 0x1831315f8 0x183130c94 0x1831309dc 0x183183770
0x180cae1e8 0x1809db1f8 0x1809f1634 0x1809f0d6c 0x1809eeac4 0x18091d680
0x181e2c088 0x185794d90 0x100183934 0x1804be8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
然后返回以下代码(突出显示底部的NSDictionary*属性
行):
此代码来自。有谁知道如何解决这个问题吗
谢谢大家! TLDR:self.highlightedTextColor
为nil
。将其设置为某种颜色
详情:
在三种情况下,代码将因此消息而崩溃:
self.font
为nil
self.textColor
为nil
和self.isEnabled
为YES
和self.isHighlighted
为NO
self.highlightedTextColor
为nil
和self.isEnabled
为YES
和self.ishighted
为YES
由于
KILabel
扩展了UILabel
,它不允许textColor
和font
使用nil
值,因此问题可能是由highlightedTextColor
(选项3)引起的。调试它,打印/检查self.font
,color
,阴影
和段落
查看哪个值为零以及为什么@jape只需将self.highlightedTextColor设置为非零值。如果(self.ishighted&&self.highlightedTextColor){或者通过提供默认的highlightedTextColor
,例如-(UIColor*)highlightedTextColor{return[super highlightedTextColor]?:[UIColor yellowColor];},您也可以在KILabel
源代码中修复它
谢谢!执行条件语句并提供默认颜色是有害的,还是毫无意义?@jape无意义。如果知道变量从不为nil
,则无需检查nil
。
- (NSDictionary *)attributesFromProperties{
// Setup shadow attributes
NSShadow *shadow = shadow = [[NSShadow alloc] init];
if (self.shadowColor){
shadow.shadowColor = self.shadowColor;
shadow.shadowOffset = self.shadowOffset;
} else {
shadow.shadowOffset = CGSizeMake(0, -1);
shadow.shadowColor = nil;
}
// Setup color attributes
UIColor *color = self.textColor;
if (!self.isEnabled){
color = [UIColor lightGrayColor];
} else if (self.isHighlighted){
color = self.highlightedTextColor;
}
// Setup paragraph attributes
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = self.textAlignment;
// Create the dictionary
NSDictionary *attributes = @{NSFontAttributeName : self.font,
NSForegroundColorAttributeName : color,
NSShadowAttributeName : shadow,
NSParagraphStyleAttributeName : paragraph,
};
return attributes;
}