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;
    }