2016-02-12 27 views
1

在我的項目,我太快速滾動,我得到一個錯誤說:嘗試插入從對象無對象[1]

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 *attributes線底部):

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

此代碼是從KILabel。有誰會碰巧知道如何解決這個問題嗎?

謝謝!

+1

調試它,打印/檢查'self.font','color','shadow'和'paragraph'來查看哪個值爲零,爲什麼! – luk2302

回答

4

TLDR:self.highlightedTextColornil。將其設置爲某種顏色。

詳情:

的代碼將這個消息三種情況崩潰:

  1. self.fontnil

  2. self.textColornil是和self.isEnabledYESself.isHighlightedNO

  3. self.highlightedTextColornilself.isEnabledYESself.isHighlightedYES

由於KILabel延伸UILabel這不允許textColorfontnil值,則問題可能是由highlightedTextColor(選項3)引起的。

+0

一個很好的答案,一個糟糕的問題! –

+1

@jape只需將'self.highlightedTextColor'設置爲非零值。您也可以在'KILabel'源文件中使用更安全的條件'else if(self.isHighlighted && self.highlightedTextColor){'或通過提供默認的'highlightedTextColor',例如' ' - (UIColor *)highlightedTextColor {return [super highlightedTextColor]?:[UIColor yellowColor]; }' – Sulthan

+0

謝謝!做條件陳述並提供默認顏色會有害嗎?還是那種毫無意義? – jape

相關問題