Created
April 7, 2017 08:01
-
-
Save nhathm/d405e08a0d6923272636d096df308042 to your computer and use it in GitHub Desktop.
Swift closure - Declare a closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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