NSRangeException on iOS 8

I upgraded XCode 5 to 6 to test my projects out on the iPhone 6 and 6 Plus and was getting cryptic crashes on startup:

'*** -[__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) 

After a LOT of trial and error and trying various XIBs in various configurations, the issue occurred because I was mixing some XIBs with Auto Layout turned on, containing other UIViews (based on XIBs) with AutoLayout off.

  • Make iOS application compatible with iPhone6 and iPhone6+ sizes
  • Image resolution for new iPhone 6 and 6+, @3x support added?
  • iPhone 6 and 6 Plus Media Queries
  • How to detect iPhone 6 & 6 Plus View Mode Programmatically
  • iPhone 6/6+ Screen size issue
  • Simulator running in scaling mode for iPhone 6 Plus?
  • Turning AutoLayout off for all of my XIBs solved the issue.

    2 Solutions Collect From Internet About “NSRangeException on iOS 8”

    Faced the same issue. Found that the problem is in button whose layer was used as mask for another button’s layer for some visual effect (CALayer‘s mask property). Removing mask button from view hierarchy fixed the problem. I just moved it to view controller scene in storyboard.

    Looks like using layer as a mask caused problem in _UIViewTopDownSubtreeTraversal function used by autolayout. Maybe other layer manipulations for some view also can.

    One solution I found is that you need to have at least 6 screens and then the app won’t crash.

    Still trying to get it to work with less screens.
    Turned off Auto Layout on my only XIB (LaunchScreen.xib) but that didn’t solve the problem.