0

サーバーからデータをロードする作業を行っていますが、サーバーが接続されていない場合があります (エラー)。

エラーアプリを回避するためにTry/Catchなどを使用したい

1: 読み込みデータで試行/キャッチ 2: イメージで試行/キャッチ

私はコードを書く方法は次のとおりです。

@try
{
 dispatch_async(htvque, ^{
        NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:listChannel]];
        NSError* error;
        jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        if (error) {
            NSLog(@"%@", error);
        }
        else
        {
             NSMutableArray *arrImage = [jsonTable objectForKey:@"ListImage"];
        for (int i =0; i<arrImage.count; i++) {
            UIImage * result;
            UIImageView *imgView = [[UIImageView alloc] init];
            imgView.frame  = CGRectMake(0, 30 * i , 20, 20);
            imgView.image = result;
            @try
            { 
               NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:                               [arrImage objectAtIndex:i]]];
               result = [UIImage imageWithData:data];
               [self.view addSubview:imgView];
            }
            @catch
            {}
            @finally
            {}


        }

        }
    });
}
@catch(NSException * exp)
{
  NSLOG(@"abc");
}
@finnaly
{

}
4

2 に答える 2

3

ObjC プログラムでは、この目的で例外を使用しないでください。

ObjC では、次の場合にのみ例外の使用を予約します。

  • あなたは回復するつもりはありません
  • そしてあなたが回復するつもりがないとき

--例外が適切である場合(私はそれらを使用しませんが、それは多くの人にとって「コア」すぎます)。

とにかく、ObjC の例外はプログラマーのエラーを示しており、論理的には (他の言語とは異なり) 回復を期待できないものです。「試して飲み込む」エラー処理ではなく、エラーが何であるかを把握してください。

私が提案する解決策は、適切なコードを示し、例外の詳細、再現方法などを示す新しい質問を作成することです。


注: 実際には、エラー処理に別のアプローチ ( NSError. 開発中に発生する Cocoa 例外の大部分は、修正する必要があり、修正できる問題です (範囲エラー、セレクターに応答しない、参照カウント)。

于 2012-10-30T18:04:44.953 に答える
1

これは、あなたが望むことを行うためのかなり堅牢な方法になると思います。この例では、try/catch に依存せずに多くのエラー チェックを表示する必要がありました。

dispatch_async(htvque, ^{
    NSError* error = nil;
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:listChannel] options:0 error:&error];
    if (error) { NSLog(@"%@", error); return; }

    NSDictionary *jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) { NSLog(@"%@", error); return; }
    if (![jsonTable isKindOfClass:[NSDictionary class]]) { NSLog(@"jsonTable is not an NSDictionary: %@", jsonTable); return; }

    NSArray *images = [jsonTable objectForKey:@"ListImage"];
    if (![images isKindOfClass:[NSArray class]]) { NSLog(@"images is not an NSArray: %@", images); return; }

    NSMutableArray *dataForImages = [NSMutableArray arrayWithCapacity:images.count];

    // Build up an array with all the image data. For simplicity sake, I'll just skip ones the fail to load.
    for (NSString *URLString in images) {
        if (![URLString isKindOfClass:[NSString class]]) { NSLog(@"URLString is not an NSString: %@", URLString); continue; }

        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString] options:0 error:&error];
        if (error) { NSLog(@"%@", error); continue; }

        [dataForImages addObject:data];
    }

    // MUST SWITCH TO MAIN QUEUE BEFORE UPDATING UI!!!!!!!
    dispatch_sync(dispatch_get_main_queue(), ^{
        // This is just a different way of iterating the array.
        [dataForImages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            UIImage *image = [UIImage imageWithData:obj];
            if (!image) { NSLog(@"Could not create image from data at index %d", idx); return; }

            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            imageView.frame = CGRectMake(0, 30 * idx , 20, 20);

            [self.view addSubview:imageView];
        }];
    });
});

これは実用的な解決策ではなく、大まかな概要です。

于 2012-10-30T18:46:41.923 に答える