Khant Thu Linn Khant Thu Linn - 1 year ago 63
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 Source

You need the following check:

range.location + range.length <= self.attributedText.length
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download