ios - dispatch_async -[__NSArrayM objectAtIndex :]: index 0 beyond bounds for empty array

标签 ios objective-c nsmutablearray dispatch-async

我无法修复此错误:*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”** *

关于如何做到这一点有什么想法吗?

看来错误是数组初始化导致的。

第一次执行该方法时,没有显示任何问题。该错误仅在第二次发生。

Dispatch_Async 方法

-(IBAction)clickRandom:(id)sender{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self RequestJson];

        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadData];
        });
    });
}

RequestJson方法

-(void)RequestJson
{
    Name = [[NSMutableArray alloc] init];
    slug = [[NSMutableArray alloc] init];
    image = [[NSMutableArray alloc] init];
    view = [[NSMutableArray alloc] init];
    category = [[NSMutableArray alloc] init];

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

        NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
        array = [jsonArray objectForKey:@"videos"];

        for (int i = 0; i < [array count]; i++)
        {
            [Name addObject:[[array objectAtIndex:i] objectForKey:@"name"]];
            [slug addObject:[[array objectAtIndex:i] objectForKey:@"slug_video"]];
            [image addObject:[[array objectAtIndex:i] objectForKey:@"thumbnail_video_original"]];
            [view addObject:[[array objectAtIndex:i] objectForKey:@"views_video"]];
            [category addObject:[[array objectAtIndex:i] objectForKey:@"category_name_video"]];

        }
       [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

崩溃日志是这样的:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                  0x2d77de7e __exceptionPreprocess + 126
1   libobjc.A.dylib                 0x37ada6c2 objc_exception_throw + 34
2   CoreFoundation                  0x2d6b3d90 -[__NSArrayM objectAtIndex:] + 228
3   Vigg                            0x00117a40 -[DEMOHomeViewController tableView:cellForRowAtIndexPath:] (DEMOHomeViewController.m:315)
4   UIKit                           0x30034310 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 404
5   UIKit                           0x2ffdc6c8 -[UITableView _updateVisibleCellsNow:] + 1796
6   UIKit                           0x2ffdbeec -[UITableView layoutSubviews] + 180
7   UIKit                           0x2ff0234e -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 342
8   QuartzCore                      0x2fb8893e -[CALayer layoutSublayers] + 138
9   QuartzCore                      0x2fb84162 CA::Layer::layout_if_needed(CA::Transaction*) + 346
10  QuartzCore                      0x2fb83ff4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 12
11  QuartzCore                      0x2fb83a08 CA::Context::commit_transaction(CA::Transaction*) + 224
12  QuartzCore                      0x2fb8381a CA::Transaction::commit() + 310
13  QuartzCore                      0x2fb7d548 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 52
14  CoreFoundation                  0x2d748f64 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16
15  CoreFoundation                  0x2d7468f2 __CFRunLoopDoObservers + 282
16  CoreFoundation                  0x2d746c3e __CFRunLoopRun + 734
17  CoreFoundation                  0x2d6b146c CFRunLoopRunSpecific + 520
18  CoreFoundation                  0x2d6b124e CFRunLoopRunInMode + 102
19  GraphicsServices                0x323eb2e6 GSEventRunModal + 134
20  UIKit                           0x2ff66840 UIApplicationMain + 1132
21  Vigg                            0x000f84c0 main (main.m:16)
22  libdyld.dylib                   0x37fd3ab2 tlv_initializer + 2

最佳答案

崩溃发生在 TableView 的数据源方法中,而不是发生在 RequestJSON 方法中(应称为 requestJSON;方法始终以小写字母开头)。

因此,您在不期望的地方有一个空数组,并且您已经将 TableView 中的行(或部分)计数与实际包含数据的数据模型对象分离。

您需要发布实际崩溃的方法的源代码。

关于ios - dispatch_async -[__NSArrayM objectAtIndex :]: index 0 beyond bounds for empty array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261856/

相关文章:

c++ - 在外部 iOS 项目中使用 Box2D/Cocos2D

objective-c - 如何判断这个内存泄漏是从哪里来的呢?

ios - 授予地址簿访问权限后,将用户联系人存储在 NSMutable 数组中

arrays - 转换可变数组在 swift 中仍然被认为是不可变的

iphone - Objective-C iPhone。获取表中的 NSMutableArray

iOS,背景图像的大小

cocoa - 在 Grand Central Dispatch 中使用 dispatch_sync

ios - UITextView 禁用文本选择

iphone - 向应用程序添加背景音乐时构建失败

ios - 从屏幕右侧向左滑动 UIView