SwiftyJD
SwiftyJD

Reputation: 5461

Syntax for adding a closure to a function?

I'm trying to create a function that has a closure. Nothing is passed to the function, just after it finishes another function has to be called. Something like this:

  func addGradient(closure: syntax) {
(closure: _ in ("function call here")   )}

so it can be called similiar to this

addGradient(closure: "function to be called")

Upvotes: 0

Views: 47

Answers (1)

user887210
user887210

Reputation:

Just use the signature for the function minus any of the names:

func doIt(one: Int, two: String) -> [String] {
  …
}

Would have the closure signature of:

(Int, String) -> [String]

So yours would be:

func addGradient(closure: (Int, String) -> [String]) {
  …
}

And you can call it like this:

addGradient(closure: doIt)

One more note, a function like this:

func doAgain() {
  …
}

Has a closure signature of this:

() -> ()

Upvotes: 2

Related Questions