Out of bounds Problem with attributedSubstringFromRange

折月煮酒 提交于 2019-12-04 02:24:11

问题


I have a NSMutableAttributedString, I need to take substring from it.I use the following code to take substring,

startIndex and endIndex are two long variables.

NSMutableAttributedString *currentString = (NSMutableAttributedString *)[attributtedString attributedSubstringFromRange:NSMakeRange(startIndex, endIndex)];

But there is an exception if index go higher. The String Length is always - 21212 I can get output when I given values (9048, 10958) But after that when I gave values (10958, 12961) there is an Exception,

NSConcreteMutableAttributedString attributedSubstringFromRange:: Out of bounds

help me in solving this issue, or give me a better way to take substring from a NSMutableAttributedString

thanks in advance,


回答1:


This isn't how NSRange works. It is location (startIndex for you) and length. Hence the second goes from 10958 to 10958 + 12961 = 23919, which is out of bounds.



来源:https://stackoverflow.com/questions/6311922/out-of-bounds-problem-with-attributedsubstringfromrange

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