Ios – Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray

iosnsarraynsrangeexceptionswift

Error in log:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray'
*** First throw call stack:
(
0 CoreFoundation 0x000000010fdd8b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001143a6141 objc_exception_throw + 48
2 CoreFoundation 0x000000010fdf027d -[__NSArray0 objectAtIndex:] + 93
3 DropInn 0x000000010d598fc4 _TFC7DropInn21ListingViewController9tableViewfTCSo11UITableView14didSelectRowAtV10Foundation9IndexPath_T_ + 5972
4 DropInn 0x000000010d599837 _TToFC7DropInn21ListingViewController9tableViewfTCSo11UITableView14didSelectRowAtV10Foundation9IndexPath_T_ + 87
5 UIKit 0x00000001122b3dcd -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1763
6 UIKit 0x00000001122b3fe3 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344
7 UIKit 0x00000001121697f3 _runAfterCACommitDeferredBlocks + 318
8 UIKit 0x00000001121567bc _cleanUpAfterCAFlushAndRunDeferredBlocks + 532
9 UIKit 0x000000011218828c _afterCACommitHandler + 137
10 CoreFoundation 0x000000010fd7e717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23
11 CoreFoundation 0x000000010fd7e687 __CFRunLoopDoObservers + 391
12 CoreFoundation 0x000000010fd63720 __CFRunLoopRun + 1200
13 CoreFoundation 0x000000010fd63016 CFRunLoopRunSpecific + 406
14 GraphicsServices 0x0000000118249a24 GSEventRunModal + 62
15 UIKit 0x000000011215d0d4 UIApplicationMain + 159
16 DropInn 0x000000010d240f47 main + 55
17 libdyld.dylib 0x0000000115fba65d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Error screenshot:

enter image description here

Source code:

let savearr : NSArray = (UserDefaults.standard.object(forKey: "savedarray") as! NSArray)

print("Savearr data ->", savearr)

let addarr: NSArray = savearr.value(forKeyPath:"country") as! NSArray
let sumarr: NSArray = savearr.value(forKeyPath:"description") as! NSArray
let titarr: NSArray = savearr.value(forKeyPath:"title") as! NSArray

appDelegate.indexrow = indexPath.row

print("addarr\(addarr)")
print("sumarr\(sumarr)")
print("titarr\(titarr)")

let MenuViewController = self.storyboard?.instantiateViewController(withIdentifier: "five") as! FiveStepsViewController

MenuViewController.writeTitleString = String(describing: titarr[indexPath.row])

MenuViewController.writeSummaryString = String(describing: sumarr[indexPath.row])

MenuViewController.writeAddressString = String(describing: addarr[indexPath.row])

//MenuViewController.writePriceString = String(describing: self.appDelegate.fivepricearray[indexPath.row])

self.present(MenuViewController, animated: true, completion: nil)

Error shows in:

MenuViewController.writeTitleString = String(describing: titarr[indexPath.row])

MenuViewController.writeSummaryString = String(describing: sumarr[indexPath.row])

MenuViewController.writeAddressString = String(describing: addarr[indexPath.row])

Best Answer

The Apple's document has said that:

NSRangeException

Name of an exception that occurs when attempting to access outside the bounds of some data, such as beyond the end of a string.

Specific in your case, one of the three array (or possibly all of them) titarr, sumarr, addarr is empty array and you're trying to access at index of an empty array => NSRangeException

To solve this problem, you can:

Check if the array is non-empty:

if !titarr.isEmpty {
    // titarr is non-empty
}

Or, for more safely, check if the index you want to access does not exceed the data limit of the array:

if titarr.count > yourIndex {
    // index is valid
}

However, I prefer direct access to the index and if it is beyond bounds, it will return nil. To do this, refer to the @kkuushkin's answer in the question.

For example, something like this:

let titarr = [1, 2, 3]

// Access the index beyond bounds, value is nil, no NSRangeException
titarr[safe: 5]
Related Topic