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.

  • How to add image and text in UITextView in IOS?
  • Bundled framework issues with Bitcode compilation
  • XCode: Displaying a UIDatePicker when user clicks on UITextbox
  • Change tab name in uitabbarcontroller
  • iOS tests will not run on simulator when using Jenkins through JNLP
  • How to bind an Objective-C static library to Xamarin.iOS?
  • 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.