attempt to insert nil object from objects[1]

jape picture jape · Feb 12, 2016 · Viewed 8k times · Source

In my project, when I scroll too quickly, I get an error saying:

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

It then returns me to the following code (highlighting the NSDictionary *attributes line towards the bottom):

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

This code is from KILabel. Would anyone happen to know how to fix this?

Thank you!

Answer

Sulthan picture Sulthan · Feb 12, 2016

TLDR: self.highlightedTextColor is nil. Set it to some color.

Details:

The code will crash with this message in three cases:

  1. self.font is nil.

  2. self.textColor is nil and self.isEnabled is YES and self.isHighlighted is NO.

  3. self.highlightedTextColor is nil and self.isEnabled is YES and self.isHighlighted is YES.

Since KILabel extends UILabel which doesn't allow nil values for textColor and font, the problem is probably caused by highlightedTextColor (option 3).