2014-11-14 19 views
1

這是發生錯誤的行。iOS:收到崩潰NSInvalidArgumentException NSConcreteMutableAttributedString addAttribute:value:range :: nil值

[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range]; 

這是完整的方法代碼。

-(void)setTextColor:(UIColor*)color range:(NSRange)range 
{ 
if (range.location != NSNotFound) { 
// kCTForegroundColorAttributeName 
[self removeAttribute:(__bridge NSString*)kCTForegroundColorAttributeName range:range]; // Work around for Apple leak 
[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range]; 
    }} 

我試過如果沒有找到範圍,但仍然發生錯誤。任何提示或建議我做錯了什麼?

回答

0

發生這種情況是因爲(__bridge id)color.CGColornil
只要做到這樣,沒有任何多餘的轉換

[self addAttribute:NSForegroundColorAttributeName value:color range:range]; 
0

它在你的情況下,錯誤檢查,您需要:

if (range.location >= 0 && range.location < self.length){ 
    if (range.location + range.length >= self.length) 
     range.length = self.length - range.location; 

    [self removeAttribute:(__bridge NSString*)kCTForegroundColorAttributeName range:range]; // Work around for Apple leak 
    [self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range]; 
}