简体   繁体   中英

Objective-C completion block in Swift

I would like to call a completion block in Swift as I have always done in Objective-C. I took a look at closures, but for some reason, I cannot get it to work...

Obj-C:

- (void)someMethodWithParam1:(NSString *)param completion:(void(^)(NSArray* arr, NSURLResponse *resp))callback
{
    callback(arr, resp);
}

Swift:

func someMethodWithParam1(param: NSString, completion:((NSArray?, NSURLResponse?)->())) ->(){
                                                                                ^
                                                                                |
                                                                  /* Error: Insert ',' here */

    completion(arr, resp)

}

EDIT

Okay, it was not a syntactical error, but an error from my side. The method header I used here is different from the one in my code. So this code that I originally posted should work fine.

Typealias's are your friend here, just to make the code more readable

typealias onComplete = (NSArray?, NSURLResponse?) -> ()

func someMethodWithParam1(param: NSString, completion:onComplete) -> ()
{

    completion(arr, resp)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM