Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40226

typealiasing closure in Swift

I am using an SDK developed in Objective-C. My Application is hybrid (support both Objc and Swift) and I need to use a completionBlock of SDK. In SDK side it was defined like,

typedef void (^SomethingCompletionBlock)(NSArray<id<Something> > *_Nullable result, NSError *_Nullable error); 

In App side I need to use like,

SDKService.fetchSomething(withModel: model) { (result, error) in

    if error != nil {

       completionBlock(result, error?)

    }
}

I wonder what would be the signature of completionBlock in my Swift part? I am trying something like below but getting error.

typealias SomethingSearchCompletionBlock = (result: Array<Something>?, error: Error?)

Error:

Cannot call value of non-function type 'SomethingSearchCompletionBlock' (aka '(result: Optional>, error: Optional)')

Upvotes: 0

Views: 430

Answers (2)

Roshan
Roshan

Reputation: 732

Might this will help

typealias SomethingCompletionBlock = (_ result: [Something]?, _ erro: Error?) -> Void

Upvotes: 2

vadian
vadian

Reputation: 285150

You have to add the Void return value and the parameter labels are not needed

typealias SomethingSearchCompletionBlock = (Array<Something>?, Error?) -> Void

Upvotes: 2

Related Questions