【问题标题】:iOS: Recieveing a crash NSInvalidArgumentException NSConcreteMutableAttributedString addAttribute:value:range:: nil valueiOS:收到崩溃 NSInvalidArgumentException NSConcreteMutableAttributedString addAttribute:value:range::nil value
【发布时间】:2014-11-14 15:40:26
【问题描述】:

这是发生错误的行。

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

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

【问题讨论】:

    标签: html ios objective-c iphone string


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在你的情况下检查错误,你需要:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 2012-01-31
        • 2020-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        相关资源
        最近更新 更多