iOS:接收崩溃NSInvalidArgumentException NSConcreteMutableAttributedString addAttribute:value:range :: nil value

时间:2014-11-14 15:40:27

标签: html ios objective-c iphone string

这是发生错误的行。

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

我尝试测试是否找不到范围,但错误仍然存​​在。关于我做错了什么的提示或建议?

2 个答案:

答案 0 :(得分:0)

这是因为(__bridge id)color.CGColornil 就这样做,没有任何额外的转换

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

答案 1 :(得分: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];
}