NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'

前端 未结 5 927
抹茶落季
抹茶落季 2020-12-16 20:32

in my application when i run app for first time,it work ok.but when i run again 2 two times, it crashes.

This is the error..

NSRangeException\', reason: \'**

相关标签:
5条回答
  • 2020-12-16 20:46

    You array is empty, but you're trying to access an object in it. That is the problem.

    0 讨论(0)
  • 2020-12-16 20:46

    In case this helps someone : in my case the array was not in code, but an IB outlet that was an array of 5 UIImageViews in the storyboard.

    @IBOutlet var upAndDownArrowImages: [UIImageView]!

    Reason for the crash was that I mistakenly deleted 1 of those UIImageViews from the Storyboard.

    0 讨论(0)
  • 2020-12-16 20:51

    In my case, it Crashes for heightForRowAt indexpath, Do check this method for smooth operation.

    Hope it Helps someone.

    0 讨论(0)
  • 2020-12-16 20:52

    Reason: According to your log, you're trying to access empty array. Just fix this by below code

    if (arrMydata.count > inxexPath.row)
        sharObj = [arrMydata objectAtIndex:indexPath.row]
    
    0 讨论(0)
  • 2020-12-16 21:02

    Reason: You are accessing Empty array about to access object at index.

    replace all places like in your code below

    [arrMydata objectAtIndex:indexPath.row];
    

    with

     //1. Positive index ([anArray objectAtIndex:-NUMBERS]) will crash
    
     //2. within the array boundary
    
     if([arrMydata count] > 0 && [arrMydata count] > indexPath.row){
    
        shrObj=[arrMydata objectAtIndex:indexPath.row];
    
     }
     else{
    
        //Array is empty,handle as you needed
    
     }
    

    **Here You can see the non software example, which will explain this issue. Good luck! **

    0 讨论(0)
提交回复
热议问题