Khant Thu Linn Khant Thu Linn - 1 month ago 6
iOS Question

Checking out of bounds for attributedSubstringFromRange

I have simple method like this.

NSAttributedString *aStr = [self.attributedText attributedSubstringFromRange:range];


But sometimes, it show error 'out of bounds'. How can I check beforehand? Do I only check .length ? Is there alternative way?

Answer

You need the following check:

range.location + range.length <= self.attributedText.length