Return a function from a function
When you want a function to return a String
, it's pretty obvious:
func whatIsForLunch() -> String {
return "Something delicious"
}
If you want to return a function, then the return type looks more like this: () -> Void
.
The key bits that trip me up:
- You need
{ }
to assign the function to a variable or constant. - You need
()
to use the assigned function
func howToMakeACake() -> (() -> Void) {
// return some function
}
var cakeRecipe: () -> Void
cakeRecipe = { howToMakeACake() } // you need the { }
if timeForCake {
cakeRecipe() // you need the () to run the function
}