SORU
11 Ocak 2011, Salı


Nasıl benim iPhone App NSError kullanabilir miyim?

Benim uygulama hataları yakalamaya çalışıyorum, ve NSError kullanarak içine arıyorum. Nasıl kullanacağını, ve bunu doldurmak için ne hakkında biraz kafam karıştı.

Ben birini sonra NSError doldurmak kullanmak nasıl bir örnek verebilir?

CEVAP
11 Ocak 2011, Salı


Ben genelde ne iyi, çalışma zamanı hatası-bu benim yöntemleri NSError bir işaretçi başvuru var. Eğer bir şey gerçekten bu yöntem ters giderse, hata veri ile NSError başvurusunu doldurmak ve yöntemi nil iade edebilirim.

Örnek:

- (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return nil;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

O zaman bu yöntemi kullanabiliriz. Hatta bu yöntem, nil çıkmadıkça hata nesnesini incelemek için zahmet etmeyin:

// initialize NSError object
NSError* error = nil;
// try to feed the world
id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!yayOrNay) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.

NSLocalizedDescriptionKey için bir değer ayarlamak için localizedDescription hata ulaştık.

Daha fazla bilgi için en iyi yer Apple's documentation. Bu gerçekten çok iyi.

Ayrıca Cocoa Is My Girlfriend güzel, basit bir öğretici olduğunu.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • andony5589

    andony5589

    7 Aralık 2011
  • Charles Griffin Gibson

    Charles Grif

    26 NİSAN 2006
  • thewinekone

    thewinekone

    17 Aralık 2005