我无法修复此错误:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' ***
关于如何做到这一点的任何想法?
似乎该错误是由数组的初始化引起的。
第一次执行该方法时,没有显示任何问题。该错误仅在第二次发生。
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