【问题标题】:Error handling when array is empty数组为空时的错误处理
【发布时间】:2013-06-17 09:23:16
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self getLoadingTableCellWithTableView:tableView];

    RssItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RssItemCell"];
    // If break in here, url fetch maybe error or row is null
    RssItem *feed = [[self.manager.feeds objectForKey:self.manager.currentChannel] objectAtIndex:indexPath.row];

    cell.title.text = feed.title;
    cell.description.text = feed.description;
    cell.date.text = feed.date;
    // if any image enclosure at rss parse
    [cell.enclosure setImage:[UIImage imageNamed:@"logo.png"]];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue,
                   ^{
                       UIImage *image = [[UIImage alloc] initWithData:feed.enclosure];
                       [cell.enclosure  setImage:image];
                       [cell setNeedsLayout];
                   });

    return cell;
}

当我尝试获取 RSS 的空行时遇到问题(我的网络类别上没有帖子)

我想显示警报并自动转到主页。

卡在这里。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self getLoadingTableCellWithTableView:tableView];

    RssItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RssItemCell"];
    @try {
        NSLog(@"Trying to tetch");
        // If break in here, url fetch maybe error or empty row
        RssItem *feed = [[self.manager.feeds objectForKey:self.manager.currentChannel] objectAtIndex:indexPath.row];
        cell.title.text = feed.title;
        cell.description.text = feed.description;
        [cell.enclosure setImage:[UIImage imageNamed:@"logo.png"]];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue,
                       ^{
                           UIImage *image = [[UIImage alloc] initWithData:feed.enclosure];
                           [cell.enclosure  setImage:image];
                           [cell setNeedsLayout];
                       });

        return cell;

    }
    @catch (NSException *e) {
        NSLog(@"catching %@ reason %@", [e name], [e reason]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@",e] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];

        TableViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
        [self presentViewController:VC animated:YES completion:nil];
    }
    @finally {
        NSLog(@"finally");
    }
}

警报未出现并转到断点。我应该如何处理这个错误?

【问题讨论】:

  • 无论数组是否为空,上面的代码都会跳转到视图控制器
  • 您是否尝试在feed == nil 上设置条件?

标签: ios objective-c error-handling nsarray


【解决方案1】:

首先,您必须始终在cellForRowAtIndexPath: 返回 UITableViewCell*。其次,这通常是一种不好的做法(请参阅下面的引用),特别是因为您可能会显示一些警报并多次显示TableViewController。作为一个问题解决方案,似乎没有例外,这让我觉得self.manager.feeds实际上是nil


Programming with Objective-C

您不应使用 try-catch 块代替标准编程 检查 Objective-C 方法。在 NSArray 的情况下,对于 例如,您应该始终检查数组的计数以确定 在尝试访问给定索引处的对象之前的项目数。 objectAtIndex: 方法会抛出异常,如果您进行 越界请求,以便您尽早发现代码中的错误 在开发周期中——你应该避免在一个 您发布给用户的应用程序。

有关 Objective-C 应用程序中异常的更多信息,请参阅 Exception Programming Topics.

【讨论】:

    猜你喜欢
    • 2011-05-05
    • 2015-08-06
    • 2019-11-28
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多