问题描述
我有一个NSMutableAttributedString,我需要从它获取子字符串。我使用以下代码来获取子字符串,
I have a NSMutableAttributedString, I need to take substring from it.I use the following code to take substring,
startIndex和endIndex是两个长变量。
startIndex and endIndex are two long variables.
NSMutableAttributedString *currentString = (NSMutableAttributedString *)[attributtedString attributedSubstringFromRange:NSMakeRange(startIndex, endIndex)];
但如果索引更高,则会有例外情况。
字符串长度总是 - 21212
当我给出值时我可以得到输出(9048,10958)
但是在我给出值(10958,12961)之后有一个异常,
But there is an exception if index go higher.The String Length is always - 21212I 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
帮助我解决这个问题,或者给我一个更好的方法从NSMutableAttributedString中获取子串
help me in solving this issue, or give me a better way to take substring from a NSMutableAttributedString
提前感谢,
推荐答案
这不是NSRange的工作原理。它是 location
(适用于你的startIndex)和长度
。因此,第二个从10958到10958 + 12961 = 23919,这是超出界限。
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.
这篇关于超出范围attributesSubstringFromRange的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!