ios - 数组为空时的错误处理

标签 ios objective-c error-handling nsarray

- (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");
    }
}

警报未出现,并转至断点。我应该如何处理此错误?

最佳答案

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

Programming with Objective-C

You should not use a try-catch block in place of standard programming checks for Objective-C methods. In the case of an NSArray, for example, you should always check the array’s count to determine the number of items before trying to access an object at a given index. The objectAtIndex: method throws an exception if you make an out-of-bounds request so that you can find the bug in your code early in the development cycle—you should avoid throwing exceptions in an app that you ship to users.

For more information on exceptions in Objective-C applications, see Exception Programming Topics.

关于ios - 数组为空时的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144227/

相关文章:

ios - 一个类应该在内部处理它自己的数据吗?

ios - 检测 UIView 上的触摸

iphone - 移除标签栏后, Storyboard 仍显示 'ghost' 标签栏项目

javascript - 一个函数出错后,Javascript函数不起作用

javascript - 发出异步请求时嵌套错误消息的正确方法

ios - Swift - 约束 - 动态标签宽度

iphone - 识别 C 变量

ios - 对于在应用程序的生命周期中应该存在的单例类来说,强保留周期是否是一个相关的考虑因素?

iphone - 等待 assetForURL block 完成

python - 希望尝试缩进