Amr Lotfy
Amr Lotfy

Reputation: 2997

Swift: how to skip unused closure arguments

Given a function signature:

func myFunc(someClosure: (argA: TypeA!, argB: TypeB!) -> Void)

sometimes I don't need the passed parameters to the closure so the call is:

myFunc { (_,_) in  
    //bla bla
}

is there a way to skip this redundant (_,_) in part?

Upvotes: 8

Views: 5491

Answers (2)

Matt H
Matt H

Reputation: 6532

Personally I don't like turning parameters into ( _ ), so I use ( _ paramName ) (Swift 3, Xcode 8.3 beta)

Upvotes: 2

Tim Vermeulen
Tim Vermeulen

Reputation: 12552

There is no way to skip it entirely, but if you won't use either argument, you can use

myFunc { _ in
    // do stuff
}

Similarly, if you only need one of the arguments, you can use

myFunc { _, something in
    // do stuff
}

Upvotes: 9

Related Questions