Gal Marom
Gal Marom

Reputation: 8629

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

Upvotes: 4

Views: 4253

Answers (2)

Add080bbA
Add080bbA

Reputation: 1876

General method signature of swift is like func name(Param1 param1:param1Type, Param2 param2:param2Type) -> returnType

Since a closure is nothing but an anonymous method, same signature rules also applies for them.

While typeAliasing a closure, you have to replace parameter names (Param1, Param2) with underScore

You can specify parameter names in closure typealias

You should use underScore before parameter names.

Then it will accept parameter names

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

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107191

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

Upvotes: 7

Related Questions