SORU
18 HAZİRAN 2009, PERŞEMBE


Nasıl Objective-C Geri aramaları gerçekleştirmek için

Objective-C bilen biri acemi bir programcı lütfen Objective-C geri arama işlevleri gerçekleştirmek için bana nasıl olduğumu

Sadece tamamlanmış bazı örnekler görmek istiyorum ve bunu anlamak gerekir.

Çok teşekkürler

CEVAP
12 EKİM 2010, Salı


StackOverflow RSS rastgele benim için soru dirilen beri bütünlüğü için, (yeni) diğer seçeneği blokları kullanmak için:

@interface MyClass: NSObject
{
    void (^_completionHandler)(int someParameter);
}

- (void) doSomethingWithCompletionHandler:(void(^)(int))handler;
@end


@implementation MyClass

- (void) doSomethingWithCompletionHandler:(void(^)(int))handler
{
    // NOTE: copying is very important if you'll call the callback asynchronously,
    // even with garbage collection!
    _completionHandler = [handler copy];

    // Do stuff, possibly asynchronously...
    int result = 5   3;

    // Call completion handler.
    _completionHandler(result);

    // Clean up.
    [_completionHandler release];
    _completionHandler = nil;
}

@end

...

MyClass *foo = [[MyClass alloc] init];
int x = 2;
[foo doSomethingWithCompletionHandler:^(int result){
    // Prints 10
    NSLog(@"%i", x   result);
}];

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Alexander Johnson

    Alexander Jo

    26 Temmuz 2008
  • Breno Rises

    Breno Rises

    7 Ocak 2014
  • Theodore Leaf

    Theodore Lea

    29 AĞUSTOS 2006