【问题标题】:NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 5 超出范围'
【发布时间】:2014-05-02 08:02:11
【问题描述】:

在我的应用程序中,当我第一次运行应用程序时,它工作正常。但是当我再次运行 2 两次时,它崩溃了。

这是错误..

NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 5 超出范围'

【问题讨论】:

  • arrMydata 数组似乎为空。这就是崩溃日志的内容。
  • arrMydata 应该是 alloc] init 或者在将对象添加到 arrMydata 之前它应该有 arrayWithCapacity://somevalue
  • @2-Stroker 我知道但我找不到解决方案。
  • @JohnDoe yes arrMydata alllock]init 在那里。任何解决方案?
  • 您是否在数组中添加了任何对象?你能显示那个代码吗

标签: ios objective-c uitableview


【解决方案1】:

原因:您正在访问空数组,即将访问索引处的对象。

替换下面代码中的所有位置

[arrMydata objectAtIndex:indexPath.row];

 //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! **

【讨论】:

  • 我认为,只需这个[arrMydata count] > inxexPath.row 支票就足够了。
  • 拥有1.正指数
  • //1.正索引 ([anArray objectAtIndex:-NUMBERS]) 会崩溃
  • 没有人不会为cellForRowAtIndexPath:创建NSIndexPath。
  • @Supertecnoboff 如果您想要非软件示例,请在此处查看。 stackoverflow.com/a/24407937/675170 。祝你好运!
【解决方案2】:

您的数组是空的,但您正试图访问其中的一个对象。这就是问题所在。

【讨论】:

    【解决方案3】:

    原因:根据您的日志,您正在尝试访问空数组。只需通过以下代码解决此问题

    if (arrMydata.count > inxexPath.row)
        sharObj = [arrMydata objectAtIndex:indexPath.row]
    

    【讨论】:

      【解决方案4】:

      如果这对某人有帮助:在我的情况下,该数组不在代码中,而是一个 IB 插座,它是故事板中 5 个 UIImageViews 的数组。

      @IBOutlet var upAndDownArrowImages: [UIImageView]!

      崩溃的原因是我错误地从情节提要中删除了其中 1 个 UIImageView。

      【讨论】:

        【解决方案5】:

        在我的情况下,它为heightForRowAt indexpath 崩溃,请检查此方法是否顺利运行。

        希望对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多