索引1超出空数组错误的边界

时间:2016-11-21 12:38:43

标签: ios iphone

我已在viewDidLoad中初始化NSMutableArray,如

playUrl = [[NSMutableArray alloc]init];

但是当我们在cellForRowAtIndexPath中使用

[playUrl insertObject:[NSString stringWithFormat:@"%@",_urlToLoad] atIndex:indexPath.row];

然后会显示此错误

2016-11-21 18:06:14.481 TraidingWins[13917:238534] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106348d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105dbcdeb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106209cc5 -[__NSArrayM insertObject:atIndex:] + 901
    3   TraidingWins                        0x0000000102dc7bd6 __45-[YouTubeVC tableView:cellForRowAtIndexPath:]_block_invoke_2 + 902
    4   TraidingWins                        0x0000000102dbb840 __58+[HCYoutubeParser h264videosWithYoutubeURL:completeBlock:]_block_invoke_2 + 48
    5   libdispatch.dylib                   0x000000010705fd9d _dispatch_call_block_and_release + 12
    6   libdispatch.dylib                   0x00000001070803eb _dispatch_client_callout + 8
    7   libdispatch.dylib                   0x00000001070681ef _dispatch_main_queue_callback_4CF + 1738
    8   CoreFoundation                      0x00000001062a20f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    9   CoreFoundation                      0x0000000106263b99 __CFRunLoopRun + 2073
    10  CoreFoundation                      0x00000001062630f8 CFRunLoopRunSpecific + 488
    11  GraphicsServices                    0x00000001087f5ad2 GSEventRunModal + 161
    12  UIKit                               0x0000000104435f09 UIApplicationMain + 171
    13  TraidingWins                        0x0000000102dcba8f main + 111
    14  libdyld.dylib                       0x00000001070b492d start + 1
    15  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

任何人都可以帮助我解决崩溃问题..

3 个答案:

答案 0 :(得分:1)

在初始化后立即将静态数据添加到数组中。如果您在UITableView方法中添加或插入,您将崩溃。这样做并检查响应

答案 1 :(得分:0)

有两种可能的解决方案:

1。您可以使用NSMutableDictionary

初​​始化:

playUrl = [[NSMutableDictionary alloc] init];

cellForRowAtIndexPath

playUrl[@(indexPath.row)] = [NSString stringWithFormat:@"%@", _urlToLoad];

2。似乎您的UITableView最初滚动了一下。所以它要求的第一个单元格是1'(不是你期望的第0个)。如果它是0,那么崩溃就会消失。

您可以仅将NSMutableArray对象插入可用位置。例如,如果您的数组包含3个对象,则可以插入0 ... 3(包括0和3)。

<强>更新

每次要将对象插入数组时,都需要检查索引中是否已存在某些内容。如果有一个对象而不是插入,则应替换(replaceObjectAtIndex:withObject:)。否则,订单将被破坏。

示例

用户向下滚动到 url3

@[ @"url1", @"url2", @"url3" ]  // everything is fine

然后向上滚动。并且表格再次请求 url2

@[ @"url1", @"url2", @"url2", @"url3" ]  // second url2 and url3 occupy wrong positions

如果您始终插入对象,则会出现此问题。如果您将 url2 替换为 url2 ,一切都会好的,数组不会更改。

答案 2 :(得分:-1)

我猜你正在点击Second Object意味着objectAtInxex 1所以,第一个索引没有对象,你直接在第一个索引插入对象。在我看来,如果你只添加或替换urlobject列表,你可以使用addObject(addbojectAtIndex Specific index)。如果你想知道任何具体的事情,请告诉我。非常感谢:)