NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds

梦想的初衷 提交于 2019-12-05 17:39:11

问题


I try to assign attributes to 3 last chars of newClock string, which is @"3:33:23".

However I get an error when construct NSRange:

NSMutableAttributedString *mas = [[NSMutableAttributedString alloc]initWithString:newClock];
[mas addAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:12]}
 range:NSMakeRange(newClock.length-3,newClock.length)];

回答1:


NSMakeRange(i, j) creates a range with location i and length j.

If for example the size of your string is 10 and your range starts in 5, and you do this:

NSMakeRange(5,10)

Your range goes from 5 to 15, so out of your string.

Try:

NSMakeRange(newClock.length-3,3)];


来源:https://stackoverflow.com/questions/19344671/nsmutablerlearray-objectatindexeffectiverange-out-of-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!