简体   繁体   中英

rangeOfComposedCharacterSequencesForRange: gives NSInvalidArgumentException

I am trying to implement read more functionality. On button click, i want to show the truncated text.

My Problem is i am getting NSInvalidArgument Exception with this code:

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

I am getting comm length in log=291 & exception as:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The index 361 is invalid'

any help would be appreciated !

The Range must not exceed the bounds of the receiver.Please read documentation link

try this:

NSRange range = [comm rangeOfComposedCharacterSequencesForRange:(NSRange){71, [comm length]-71}];
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM