终止应用程序,由于未捕获的exception“NSRangeException”,原因:'*** – :索引2超出空NSArray的界限

日志中的错误:

*由于未捕获exception'NSRangeException',原因:'* – [ NSArray0 objectAtIndex:]:index 2超出了空NSArray的界限'*** 终止应用程序 ***第一个抛出调用堆栈:(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:animation:的scrollPosition:notifyDelegate:] + 1763 6的UIKit 0x00000001122b3fe3 – [ UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344 7 UIKit 0x00000001121697f3 _runAfterCACommitDeferredBlocks + 318 8 UIKit 0x00000001121567bc _cleanUpAfterCAFlushAndRunDeferredBlocks + 532 9 U IKit 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主+ 55 17 libdyld。 dylib 0x0000000115fba65d start + 1)libc ++ abi.dylib:以NSExceptiontypes的未捕获exception终止

错误截图:

在这里输入图像说明

源代码:

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) 

错误显示在:

 MenuViewController.writeTitleString = String(describing: titarr[indexPath.row]) MenuViewController.writeSummaryString = String(describing: sumarr[indexPath.row]) MenuViewController.writeAddressString = String(describing: addarr[indexPath.row]) 

 if ( titarr.count > 0 ) { // rest of your code } 

苹果的文件曾经说过:

NSRangeException

尝试访问某些数据的边界之外(例如超出string末尾)时发生的exception的名称。

具体在你的情况下,三个数组中的一个(或可能是所有三个) titarrsumarraddarr是空数组,你试图访问一个空数组的索引=> NSRangeException

要解决这个问题,你可以:

检查数组是否非空

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

或者,为了更安全起见,请检查您要访问的索引是否不超过数组的数据限制:

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

但是,我更喜欢直接访问索引,如果超出范围,它将返回nil 要做到这一点,请参考@kkuushkin在问题中的回答 。

例如,像这样的东西:

 let titarr = [1, 2, 3] // Access the index beyond bounds, value is nil, no NSRangeException titarr[safe: 5]