'iOS: Recieveing a crash NSInvalidArgumentException NSConcreteMutableAttributedString addAttribute:value:range:: nil value
This is the line that the error is occurring on.
[self addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)color.CGColor range:range];
This is the full method code.
-(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];
}}
I tried testing if range is not found, but the error still occured. Any tips or suggestions on what I did wrong?
Solution 1:[1]
That happens because (__bridge id)color.CGColor
is nil
.
Just do like this, without any extra conversions
[self addAttribute:NSForegroundColorAttributeName value:color range:range];
Solution 2:[2]
it wrong checking in yours case, you need:
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];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | l0gg3r |
Solution 2 | mityaika07 |