![](/img/trans.png)
[英]Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
[英]Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray
日志錯誤:
*由於未捕獲的異常'NSRangeException'而終止應用程序,原因:'* -[ NSArray0 objectAtIndex:]:空NSArray的索引2超出范圍。'***第一個調用堆棧:(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:動畫:的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:以類型為NSException的未捕獲異常終止
錯誤截圖:
源代碼:
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])
蘋果的文件說:
NSRangeException
嘗試在某些數據范圍之外(例如,字符串末尾)訪問時發生的異常的名稱。
在您的情況下,三個數組之一(或全部可能是其中之一) titarr
, sumarr
, addarr
是空數組,並且您嘗試訪問空數組的索引=> 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]
if ( titarr.count > 0 ) {
// rest of your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.