Gal Marom Gal Marom - 1 month ago 9
Swift Question

Defining typealias in Swift 3 for closures

I'm trying to define a type alias for a closure in swift 3 like that:

public typealias URLRequestClosure = (response: URLResponse?, data: Data?, error: Error?) -> Void


I get an error that I'm supposed to put underscore before the parameters' names. i.e:
public typealias URLRequestClosure = (_ response: URLResponse?, _ data: Data?, _ error: Error?) -> Void

can anyone explain me why? Is it something to do with Swift 3?

Thanks a lot

Answer

You can't specify parameter names in closure typealias. So instead of:

public typealias URLRequestClosure = (response: URLResponse?, data: Data?, error: Error?) -> Void

You should use:

public typealias URLRequestClosure = (URLResponse?, Data?, Error?) -> Void
Comments