由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** - [__ NSArray0 objectAtIndex:]:索引2超出空NSArray

时间:2017-07-12 07:20:40

标签: ios swift nsarray nsrangeexception

日志错误:

  

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [ NSArray0 objectAtIndex:]:索引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: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:以NSException类型的未捕获异常终止

错误截图:

enter image description here

源代码:

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])

2 个答案:

答案 0 :(得分:4)

Apple's document已经说过:

  

<强> NSRangeException

     

尝试访问外部时发生的异常的名称   某些数据的边界,例如超出字符串的末尾。

在您的情况下,三个数组中的一个(或可能全部)titarrsumarraddarr中的一个是空数组,并且您正在尝试访问空数组的索引=&gt;的 NSRangeException

要解决此问题,您可以:

检查数组是否为非空:

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

或者,为了更安全,请检查您要访问的索引是否未超出阵列的数据限制:

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

但是,我更喜欢直接访问索引,如果它超出范围,它将返回nil。为此,请参阅@kkuushkin中的the question答案。

例如,像这样:

let titarr = [1, 2, 3]

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

答案 1 :(得分:2)

if ( titarr.count > 0 ) {
   // rest of your code
}
相关问题