获取异常"NSRangeException" [英] Getting Exception 'NSRangeException'

查看:111
本文介绍了获取异常"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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆