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

StackOverflow https://stackoverflow.com/questions/19975976

  •  30-07-2022
  •  | 
  •  

Question

I'm new to IOS development. When i run my app ,i got the following error. Could anyone help me..

I have placed an UITextView inside a TableViewCell. And i have set 'one\n' as text of the UITextView.

My Code :

- (id)initWithFrame:(CGRect)frame withIndexPath:(NSArray *)indexPath
{
    self = [super initWithFrame:frame];
    if (self) {
        mIndexPath = indexPath;
        mRange = NSMakeRange(0,4);

        self.delegate = self;
        self.attributedText = @"one\n";     
        self.scrollEnabled = NO;
    }
    return self;
}

- (void)textViewDidBeginEditing:(TextView *)textView
{
     [self updateRangeForSelection];
}

- (void)updateRangeForSelection
{
   NSRange selectedRange = self.selectedRange;
   NSLog(@"%d",selectedRange.location);
}

When i tap on the UITextView, I got the selected Range as (3,0). After i logging the selected Range, Myself got the following exception.

ExCeption:

2013-11-14 16:16:24.690 ZohoWriter[484:60b] ARRAY : (
0   CoreFoundation                      0x2d488e9b <redacted> + 154
1   libobjc.A.dylib                     0x37c3d6c7 objc_exception_throw + 38
2   CoreFoundation                      0x2d488dc5 <redacted> + 0
3   Foundation                          0x2de6911d <redacted> + 88
4   UIKit                               0x2fdaee89 <redacted> + 328
5   UIFoundation                        0x35038cd9 <redacted> + 36
6   UIKit                               0x2fdaeceb <redacted> + 218
7   UIKit                               0x2fdaeb79 <redacted> + 1128
8   UIFoundation                        0x3501dde3 <redacted> + 46
9   UIKit                               0x2fdae67b <redacted> + 222
10  UIKit                               0x2fdae533 <redacted> + 54
11  UIKit                               0x2fdae4d9 <redacted> + 132
12  UIKit                               0x2fdac5f3 <redacted> + 2146
13  UIKit                               0x2fd93739 <redacted> + 196
14  UIKit                               0x2fc3e18b <redacted> + 1138
15  UIKit                               0x2ffc9d4f <redacted> + 46
16  UIKit                               0x2fc055cf <redacted> + 218
17  UIKit                               0x2fc03d33 <redacted> + 298
18  UIKit                               0x2fc3c9fd <redacted> + 772
19  UIKit                               0x2fc3c3ab <redacted> + 666
20  UIKit                               0x2fc11d79 <redacted> + 196
21  UIKit                               0x2fc10569 <redacted> + 7116
22  CoreFoundation                      0x2d453f1f <redacted> + 14
23  CoreFoundation                      0x2d4533e7 <redacted> + 206
24  CoreFoundation                      0x2d451bd7 <redacted> + 630
25  CoreFoundation                      0x2d3bc471 CFRunLoopRunSpecific + 524
26  CoreFoundation                      0x2d3bc253 CFRunLoopRunInMode + 106
27  GraphicsServices                    0x320d02eb GSEventRunModal + 138
28  UIKit                               0x2fc71845 UIApplicationMain + 1136
29  ZohoWriter                          0x000bea1d main + 116
30  libdyld.dylib                       0x38136ab7 <redacted> + 2
)
2013-11-14 16:16:24.695 ZohoWriter[484:60b] Exception : The index 4 is invalid
2013-11-14 16:16:24.696 ZohoWriter[484:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The index 4 is invalid'

terminating with uncaught exception of type NSException
Program received signal:  “SIGABRT”.

What i'm doing wrong here ?

Was it helpful?

Solution

The provided code is not the place where it's crashing. Somewhere else you are using a range with index 4,, but that range is exceeding the bounds of the (attributed)string(?). You can use enable exception breakpoints in Xcode to see where that actually happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top