【问题标题】:App crash due to empty array由于空数组导致应用程序崩溃
【发布时间】:2018-07-22 07:33:59
【问题描述】:
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:]: index 0 beyond bounds for empty array” * 首先抛出调用栈: (0x182caa364 0x181ef0528 0x182c42e9c 0x182b72820 0x100858628 0x100857624 0x18c279670 0x18c279bdc 0x18c31ee74 0x18c3e58b0 0x18c371910 0x18c4b5c98 0x18c299240 0x18c298bb4 0x18c2988c0 0x18c298960 0x186cfce14 0x1010a528c 0x1010a9ea0 0x182c52544 0x182c50120 0x182b6fe58 0x184a1cf84 0x18c2c467c 0x10085e320 0x18268c56c) libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 你的回复是怎么来的?
  • 向我们展示您遇到崩溃的代码。在collectionview方法中?
  • 你在哪里分配 arrRestaurtentTypeList 并且你确定你在 [response objectForKey:@"Main Category"] 中得到数据,因为你得到了什么错误它是自我回答吗?您只需要检查为什么该数组为空。大多数情况下,因为你们没有初始化
  • 我在 viewDidLoad 中初始化了它

标签: ios objective-c json xcode nsarray


【解决方案1】:
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];

                    });

【讨论】:

  • 我在collectionview的didSelected方法中添加了断点
  • 现在查看编辑并让我知道它是否有效并感谢 Karthik
  • 网络连接不正常时会出现此问题
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多