ios - 由于空数组导致应用程序崩溃

标签 ios objective-c json xcode nsarray

NSString *strMethod;
NSString *strType = @"restaurant";
NSDictionary *params = @{@"type": strType};
NSString *strMethod = [NSString stringWithFormat:@"%@%@", API_CATEGORY_SHOP, @"en"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 manager.responseSerializer = [AFJSONResponseSerializer serializer];
 NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:[NSSet setWithObject:@"text/json"]];
 [contentTypes addObject:@"text/html"];
 [contentTypes addObject:@"application/json"];

 manager.responseSerializer.acceptableContentTypes = contentTypes;
 [manager POST:strMethod parameters:params progress:nil success:^(NSURLSessionDataTask *task, id response){

    if ([[response objectForKey:@"result"] intValue] == 1) {

        [_arrRestaurtentTypeList removeAllObjects];
        [_arrRestaurtentTypeList addObjectsFromArray:[response objectForKey:@"Main Category"]];

        NSDictionary *dictAll = [_arrRestaurtentTypeList lastObject];

        if (dictAll) {
            [_arrRestaurtentTypeList removeLastObject];
            [_arrRestaurtentTypeList insertObject:dictAll atIndex:0];
        }

        [_collRestaurentTypeList reloadData];
        [_tblRestaurent reloadData];

    }

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    ;
}];
}

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围” * 首先抛出调用堆栈: (0x182caa364 0x181ef0528 0x182c42e9c 0x182b72820 0x100858628 0x100857624 0x18c279670 0x18c279bdc 0x18c31ee74 0x18c3e58b0 0x18 c371910 0x18c4b5c98 0x18c299240 0x18c298bb4 0x18c2988c0 0x18c298960 0x186cfce14 0x1010a528c 0x1010a9ea0 0x182c52544 0x182c5012 0 0x182b6fe58 0x184a1cf84 0x18c2c467c 0x10085e320 0x18268c56c) libc++abi.dylib:以 NSException 类型的未捕获异常终止

最佳答案

if ([[response objectForKey:@"result"] intValue] == 1) {

    [_arrRestaurtentTypeList removeAllObjects];
    [_arrRestaurtentTypeList addObjectsFromArray:[response objectForKey:@"Main Category"]];

    NSDictionary *dictAll = [_arrRestaurtentTypeList lastObject];

    if (dictAll) {
        [_arrRestaurtentTypeList removeLastObject];
        [_arrRestaurtentTypeList insertObject:dictAll atIndex:0];
    }

    [_collRestaurentTypeList reloadData];
    [_tblRestaurent reloadData];

}

用下面的代码改变这一行

 if ([[response valueForKey:@"result"] count] >0){
for (NSDictionary *dict in [response valueForKey:@"result"]) {
                        NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
                        [newDict addEntriesFromDictionary:dict];
                        [_arrRestaurtentTypeList addObject:newDict];
                    }
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [_tblRestaurent reloadData];

                    });

关于ios - 由于空数组导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741261/

相关文章:

ios - iOS 开发中 Info.plist 变量的最佳实践是什么?

iphone - 从其他 View Controller 更改 UIView(在 Nib 内)背景

objective-c - 重新启动至训练营(xcode OSX)

objective-c - Objective-C 方法的语义

c# - 在 iOS 中使用 WKWebView 请求桌面站点

Json-Opening Yelp Data Challenge的数据集

android - 无法显示 JSON 数组,JSONException : No value Error

javascript - Jquery 自动完成来自远程 JSON 的多个值

ios - Swift 3 不正确的初始 UITableViewCell 行高

ios - 两个类,相同的属性名称,不同的类型 -> 被覆盖