NSMutableAttributing相关 ios7升级ios8出现的问题

项目在ios7上没有问题,但是升级ios8以后,出现了不少问题,其中一个追踪到就是在NSMutableAttributing的使用上出现的问题。

项目中有个功能是:有一串字符串,对指定位置的字符设置不同的颜色,就是使用NSMutableAttributedString来实现的:

NSMutableAttributedString *attibuttionStr = [[NSMutableAttributedString alloc] initWithString:str];

[attibuttionStr addAttribute:(NSString *)NSForegroundColorAttributeName

                           value:(id)firstColor.CGColor

                           range:oneRange];

在ios7上这样是能正常实现的

但是ios8上除了问题,程序崩溃,提示“

2014-09-23 13:30:50.224 RecordForStudy[17993:413448] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType set]: unrecognized selector sent to instance 0x7fcbcba07090'

最终找出,

在ios8上用value:(id)firstColor.CGColor就会报错,

改成value:(id)firstColor就好了。

[attibuttionStr addAttribute:(NSString *)NSForegroundColorAttributeName

                           value:(id)firstColor

                           range:oneRange];

这样就好了。

总结:在ios7以及之前,value的值写

1、(id)firstColor.CGColor

或者直接写

2、(id)firstColor

都是没问题的,

但是在ios8上只能写成2、(id)firstColor这种样式才是正确的

原文地址:https://www.cnblogs.com/guatiantian/p/3988184.html