Declare a block method parameter without using a typedef

前端 未结 5 624
感情败类
感情败类 2020-12-04 05:13

Is it possible to specify a method block parameter in Objective-C without using a typedef? It must be, like function pointers, but I can\'t hit on the winning syntax witho

5条回答
  •  暖寄归人
    2020-12-04 06:04

    Another example (this issue benefits from multiple):

    @implementation CallbackAsyncClass {
    void (^_loginCallback) (NSDictionary *response);
    }
    // …
    
    
    - (void)loginWithCallback:(void (^) (NSDictionary *response))handler {
        // Do something async / call URL
        _loginCallback = Block_copy(handler);
        // response will come to the following method (how is left to the reader) …
    }
    
    - (void)parseLoginResponse {
        // Receive and parse response, then make callback
    
       _loginCallback(response);
       Block_release(_loginCallback);
       _loginCallback = nil;
    }
    
    
    // this is how we make the call:
    [instanceOfCallbackAsyncClass loginWithCallback:^(NSDictionary *response) {
       // respond to result
    }];
    

提交回复
热议问题