iOS 8上的NSRangeException

我将XCode 5升级到6,以便在iPhone 6和6 Plus上测试我的项目,并在启动时遇到神秘的崩溃:

'*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]' *** First throw call stack: ( 0 CoreFoundation 0x000000010569b3f5 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000104b20bb7 objc_exception_throw + 45 2 CoreFoundation 0x00000001055864d3 -[__NSArrayM objectAtIndex:] + 227 3 UIKit 0x00000001028164e1 _UIViewTopDownSubtreeTraversal + 193 4 UIKit 0x0000000102e43286 -[UIView(UIConstraintBasedLayout_EngineDelegate) _invalidateSystemLayoutSizeFittingSizeAtEngineDelegateLevel] + 128 5 Foundation 0x0000000103aa66df -[NSISEngine tryToAddConstraintWithMarker:expression:integralizationAdjustment:mutuallyExclusiveConstraints:] + 915 6 Foundation 0x0000000103c2f678 -[NSLayoutConstraint _addLoweredExpression:toEngine:integralizationAdjustment:lastLoweredConstantWasRounded:mutuallyExclusiveConstraints:] + 275 7 Foundation 0x0000000103a9ae3a -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 220 8 UIKit 0x0000000102e40495 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474 9 Foundation 0x0000000103aa8a8e -[NSISEngine withBehaviors:performModifications:] + 155 10 UIKit 0x0000000102e4029b __57-[UIView(AdditionalLayoutSupport) 

经过大量的试验和错误以及在各种配置中尝试各种XIB之后,问题就出现了,因为我在打开自动布局时混合了一些XIB,包含其他UIViews(基于XIB)并关闭了AutoLayout。

关闭所有XIB的AutoLayout解决了这个问题。

面对同样的问题。 发现问题出现在按钮中,其图层被用作另一个按钮层的遮罩,以获得一些视觉效果( CALayer的遮罩属性)。 从视图层次中删除掩码按钮修复了问题。 我只是将它移动到故事板中查看控制器场景。

看起来像使用图层作为掩码导致_UIViewTopDownSubtreeTraversal使用的_UIViewTopDownSubtreeTraversal函数出现问题。 也许某些视图的其他图层操作也可以。

我找到的一个解决方案是你需要至少有6个屏幕然后应用程序不会崩溃。

仍然试图让它用更少的屏幕工作。 在我唯一的XIB(LaunchScreen.xib)上关闭了自动布局,但这并没有解决问题。