Skip to content

Instantly share code, notes, and snippets.

@nhathm
Created April 7, 2017 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhathm/d405e08a0d6923272636d096df308042 to your computer and use it in GitHub Desktop.
Save nhathm/d405e08a0d6923272636d096df308042 to your computer and use it in GitHub Desktop.
Swift closure - Declare a closure
import Foundation
// Declare a variable to hold a closure
var add: (Int, Int) -> Int
// Assign a closure to a variable
add = { (a: Int, b: Int) -> Int in
return a + b
}
// Or combine like this
var sub = { (a: Int, b: Int) -> Int in
return a - b
}
add(1, 2)
sub(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment