iOS未捕获的异常添加子视图崩溃

Posted

技术标签:

【中文标题】iOS未捕获的异常添加子视图崩溃【英文标题】:iOS uncaught exception adding subview crash 【发布时间】:2015-06-07 08:52:15 【问题描述】:

在我的 ios 应用程序中,我将 Swift 和 Obj-c 与外部库一起使用,但由于某种原因,当库添加子视图时我遇到了崩溃。

[view addSubview:self];

堆栈跟踪如下,由于它不在顶部附近(我认为),因此很难跟踪错误。

*** Terminating app due to uncaught exception 'NSRangeException', reason: 

'*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010913ac65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108dd3bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000109024093 -[__NSArrayM objectAtIndex:] + 227
    3   UIKit                               0x0000000106fa1881 _UIViewTopDownSubtreeTraversal + 193
    4   UIKit                               0x00000001075c2fa3 -[UIView(UIConstraintBasedLayout_EngineDelegate) _invalidateSystemLayoutSizeFittingSizeAtEngineDelegateLevel] + 141
    5   Foundation                          0x00000001065e4d6f -[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:] + 915
    6   Foundation                          0x000000010676f1e8 -[NSLayoutConstraint _addLoweredExpression:toEngine:integralizationAdjustment:lastLoweredConstantWasRounded:mutuallyExclusiveConstraints:] + 275
    7   Foundation                          0x00000001065d949a -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 220
    8   UIKit                               0x00000001075bfa34 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
    9   Foundation                          0x00000001065e71be -[NSISEngine withBehaviors:performModifications:] + 155
    10  UIKit                               0x00000001075bf83a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    11  UIKit                               0x00000001075bf64d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    12  UIKit                               0x00000001075bf933 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
    13  Foundation                          0x00000001065e71be -[NSISEngine withBehaviors:performModifications:] + 155
    14  UIKit                               0x00000001075bf83a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
    15  UIKit                               0x00000001075bf64d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
    16  UIKit                               0x00000001075bf2de -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
    17  UIKit                               0x00000001075b426e -[UIView(UIConstraintBasedLayout) _layoutEngine_windowDidChange] + 126
    18  UIKit                               0x0000000106fbf561 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 209
    19  UIKit                               0x0000000106fb7ecf __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
    20  UIKit                               0x0000000106fb7e43 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
    21  UIKit                               0x0000000106fc1f48 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1660
    22  UIKit                               0x0000000106f3765a -[_UIParallaxDimmingView didMoveToWindow] + 123
    23  UIKit                               0x0000000106fbfa68 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1496
    24  UIKit                               0x0000000106fbf74a -[UIView(Internal) _didMoveFromWindow:toWindow:] + 698
    25  UIKit                               0x0000000106fb7ecf __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 125
    26  UIKit                               0x0000000106fb7e43 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
    27  UIKit                               0x0000000106fc1f48 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1660
    28  UIKit                               0x0000000106f33f08 __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 2101
    29  UIKit                               0x0000000106fbc68e +[UIView(Animation) performWithoutAnimation:] + 65
    30  UIKit                               0x0000000106f33342 -[_UINavigationParallaxTransition animateTransition:] + 1225
    31  UIKit                               0x000000010709abfc -[UINavigationController _startCustomTransition:] + 3038
    32  UIKit                               0x00000001070a63bf -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
    33  UIKit                               0x00000001070a6f0e -[UINavigationController __viewWillLayoutSubviews] + 43
    34  UIKit                               0x00000001071f1715 -[UILayoutContainerView layoutSubviews] + 202
    35  UIKit                               0x0000000106fc4a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    36  QuartzCore                          0x00000001087e3ec2 -[CALayer layoutSublayers] + 146
    37  QuartzCore                          0x00000001087d86d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    38  QuartzCore                          0x00000001087d8546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    39  QuartzCore                          0x0000000108744886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    40  QuartzCore                          0x0000000108745a3a _ZN2CA11Transaction6commitEv + 462
    41  QuartzCore                          0x00000001087460eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    42  CoreFoundation                      0x000000010906dca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    43  CoreFoundation                      0x000000010906dc00 __CFRunLoopDoObservers + 368
    44  CoreFoundation                      0x0000000109063a33 __CFRunLoopRun + 1123
    45  CoreFoundation                      0x0000000109063366 CFRunLoopRunSpecific + 470
    46  GraphicsServices                    0x000000010b1bfa3e GSEventRunModal + 161
    47  UIKit                               0x0000000106f44900 UIApplicationMain + 1282
    48  HedgeEm                             0x0000000104f1cfdf main + 111
    49  libdyld.dylib                       0x0000000109cff145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的第一个想法是寻找任何使用数组的东西,但在我的代码中没有任何东西,我认为它与 iOS 添加子视图但失败有关。 进一步调查,相同的实现在 ios 7 中有效,我知道它与可能的约束问题有关 - 可能是添加的编译时间约束。我想这适合我使用旧图书馆。

【问题讨论】:

在这种情况下 self 是什么样的类? 新的子视图有什么限制吗?当 Cocoa 尝试访问约束以布局子视图时,堆栈跟踪显示数组边界异常。 self 是一个 EAIntroView,它是一个 UIView。没有视图限制,我故意将它们全部删除,因为我认为是同一个保罗。 进一步调查,相同的实现在 ios 7 中有效,我知道它与可能的约束问题有关 - 可能是添加的编译时间约束。我想这适合我使用旧图书馆。 分享代码详情 【参考方案1】:

我使用的是 EAIntroView 库 EAIntroView - GitHub 原来是这个库有问题,最新版本导致iOS 8崩溃,GitHub - crash discussion所以解决办法就是降级到2.6就可以了。

【讨论】:

以上是关于iOS未捕获的异常添加子视图崩溃的主要内容,如果未能解决你的问题,请参考以下文章

“由于未捕获的异常而终止应用程序”在推送视图控制器时崩溃

iOS 11 - 在 VKSideMenu 中添加子视图时崩溃

如何在 swift 中为 ios 的 google maps sdk 中的地图视图添加标记

当视图未将自身添加到其子视图时,“无法将自身添加为子视图”崩溃(带有示例代码)

当视图模型@Published 更改时,SwiftUI 列表因“NSRangeException”而崩溃

无法激活约束