rangeOfComposedCharacterSequencesForRange:给出NSInvalidArgumentException

时间:2013-10-28 11:23:37

标签: ios objective-c

我正在尝试实现阅读更多功能。单击按钮,我想显示截断的文本。

我的问题我收到此代码的NSInvalidArgument Exception:

-(IBAction) ReadMore:(id)sender
{
NSLog(@"%d",[comm length]);
NSRange range = [comm rangeOfComposedCharacterSequencesForRange:(NSRange){71, [comm length]}];
truncString = [comm substringWithRange:range];
NSLog(@"Welcome=%@",truncString);
}

我在log = 291&例外:

  

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'索引361无效'

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

范围不得超过接收者的范围。请阅读文档link

试试这个:

NSRange range = [comm rangeOfComposedCharacterSequencesForRange:(NSRange){71, [comm length]-71}];

答案 1 :(得分:0)

    NSString *comm = @"audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set audio session category set";
        NSLog(@"%lu",(unsigned long)[comm length]);
        NSRange range = [comm rangeOfComposedCharacterSequencesForRange:(NSRange){71, [comm length]-71}];
        NSString  *truncString = [comm substringWithRange:range];
        NSLog(@"Welcome=%@",truncString);

Range should be within string length.