UIViewController.init(rootViewController :)崩溃

时间:2018-10-30 07:59:47

标签: ios swift

我的应用程序确实在每次UIViewController.init(rootViewController:)调用时崩溃。 如果我将其替换为:

let nav = UINavigationController.init()
nav.addChildViewController(vc)

然后它可以工作,但是我不能更改依赖关系。因此,我需要对此进行适当的修复。

示例代码:

func showSettingScreen() {
    let vc = MAIN_STORYBOARD().instantiateViewController(withIdentifier: "SettingViewController")
    self.window?.rootViewController?.present(UINavigationController(rootViewController: vc), animated: true, completion: nil)
}

崩溃:

2018-10-30 08:43:14.360304+0100 App[16881:5287132] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010b75129b __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010a55f735 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010b62b20d -[__NSArray0 objectAtIndex:] + 93
    3   libswiftCore.dylib                  0x000000010be93cb2 $Ss12_ArrayBufferV19_getElementSlowPathyyXlSiF + 146
    4   libswiftCore.dylib                  0x000000010bead0b6 $SSa11_getElement_20wasNativeTypeChecked22matchingSubscriptCheckxSi_Sbs16_DependenceTokenVtF + 118
    5   libswiftCore.dylib                  0x000000010beacf92 $SSayxSicig + 130
    6   App                               0x00000001034a2c84 $SSo22UINavigationControllerC5AppE28setNeedsNavigationBackground33_B4C9FD2C89DE46C2041196DCEE1BF2ACLL5alphay12CoreGraphics7CGFloatV_tF + 244
    7   App                               0x00000001034a52b5 $SSo22UINavigationControllerC5AppE13navigationBar_10shouldPushSbSo0aE0C_So0A4ItemCtF + 229
    8   App                               0x00000001034a53e1 $SSo22UINavigationControllerC5AppE13navigationBar_10shouldPushSbSo0aE0C_So0A4ItemCtFTo + 81
    9   UIKitCore                           0x000000011353d797 -[UINavigationBar _pushNavigationItem:transitionAssistant:] + 128
    10  UIKitCore                           0x000000011353d0af -[UINavigationBar _pushNavigationItemUsingCurrentTransition:] + 368
    11  UIKitCore                           0x000000011353d214 -[UINavigationBar pushNavigationItem:animated:] + 179
    12  UIKitCore                           0x0000000113537a50 -[UINavigationBar _performUpdatesIgnoringLock:] + 45
    13  UIKitCore                           0x000000011353d30b -[UINavigationBar _pushNavigationItem:transition:] + 133
    14  UIKitCore                           0x000000011356de53 __71-[UINavigationController pushViewController:transition:forceImmediate:]_block_invoke + 363
    15  UIKitCore                           0x000000011356c040 -[UINavigationController _executeNavigationHandler:deferred:] + 290
    16  UIKitCore                           0x000000011356dbcf -[UINavigationController pushViewController:transition:forceImmediate:] + 2083
    17  UIKitCore                           0x000000011356d12e -[UINavigationController pushViewController:animated:] + 681
    18  UIKitCore                           0x000000011354cc9d -[UINavigationController initWithRootViewController:] + 123
    19  App                               0x000000010337505f $SSo22UINavigationControllerC08rootViewB0ABSo06UIViewB0C_tcfcTO + 31
    20  App                               0x0000000103364ff7 $SSo22UINavigationControllerC08rootViewB0ABSo06UIViewB0C_tcfC + 39
    21  App                               0x0000000103364f72 $S5App11AppDelegateC17showSettingScreenyyF + 434
    22  App                               0x00000001033e328e $S5App22SideMenuViewControllerC05tableD0_14didSelectRowAtySo07UITableD0C_10Foundation9IndexPathVtF + 2910
    23  App                               0x00000001033e34bb $S5App22SideMenuViewControllerC05tableD0_14didSelectRowAtySo07UITableD0C_10Foundation9IndexPathVtFTo + 107
    24  UIKitCore                           0x00000001137d3d29 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1813
    25  UIKitCore                           0x00000001137d3f44 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 344
    26  UIKitCore                           0x000000011328a97e _runAfterCACommitDeferredBlocks + 318
    27  UIKitCore                           0x0000000113279424 _cleanUpAfterCAFlushAndRunDeferredBlocks + 384
    28  UIKitCore                           0x00000001132a8870 _afterCACommitHandler + 135
    29  CoreFoundation                      0x000000010b6b4037 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    30  CoreFoundation                      0x000000010b6ae4ce __CFRunLoopDoObservers + 430
    31  CoreFoundation                      0x000000010b6aeb61 __CFRunLoopRun + 1537
    32  CoreFoundation                      0x000000010b6ae221 CFRunLoopRunSpecific + 625
    33  GraphicsServices                    0x000000010dad91dd GSEventRunModal + 62
    34  UIKitCore                           0x000000011327f115 UIApplicationMain + 140
    35  App                               0x0000000103374634 main + 68
    36  libdyld.dylib                       0x000000010cacf551 start + 1
    37  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

它确实在iOS 12上的模拟器和真实设备上发生。有了iOS 11.4,一切正常。

2 个答案:

答案 0 :(得分:0)

It appears you are getting an array out of bounds exception which means somewhere your code is accessing a nonexistent index in an array.

Add an exception breakpoint, then recreate the crash. Xcode will then show you where the exception is occurring.

enter image description here

答案 1 :(得分:0)

查看崩溃打印日志中的句子:

  

由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范围”。

这意味着您的代码数组访问的元素超出了其最大索引值。