获取异常"NSRangeException" [英] Getting Exception 'NSRangeException'
问题描述
在以下方法中,substringWithRange:range出现异常.
I am getting Exception for substringWithRange:range in below method.
我正在禁用编辑的textview.
I am having textview with editing disabled.
我仅将文本字段用于文本选择. 当我为第一时间选择文本时,没有例外,但是当我第二次按下它时.
i am using textfield only for text selection. when i am selecting text for firs time no exception but when i press for second time it throughs.
例外: 'NSRangeException',原因:' * -[NSCFString substringWithRange:]:范围或索引超出范围".
Exception: 'NSRangeException', reason: '* -[NSCFString substringWithRange:]: Range or index out of bounds'.
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSRange range = [tv selectedRange];
str = [tv.text substringWithRange:range];
}
推荐答案
我已经检查了您的示例.有时您会检索到未定义的范围,例如(2147483647,0).因此,请检查它以避免崩溃:
I've checked your example. Sometimes you retrieve an undefined range like (2147483647, 0). So, check it to avoid crashes:
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSRange range = [textView selectedRange];
if(range.length == 0 || range.location > textView.text.length)
return;
NSString *str = [textView.text substringWithRange:range];
}
这篇关于获取异常"NSRangeException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!