Skip to content

Instantly share code, notes, and snippets.

@sketchytech
Last active November 21, 2016 15:13
Show Gist options
  • Save sketchytech/bf202a2e5676964a851aad541e074de4 to your computer and use it in GitHub Desktop.
Save sketchytech/bf202a2e5676964a851aad541e074de4 to your computer and use it in GitHub Desktop.
Swift: regular closure vs auto closure
// a regular method that doesn't have any arguments
func regularClosure(str:() -> String) {
str()
}
regularClosure{"print me!"}
// can be replaced with an @autoclosure
func autoClosure(@autoclosure str:() -> String) {
str()
}
autoClosure("print me!")
// but one that takes an argument cannot
func regularClosureWithArgument(str:(String) -> String) {
str("print me!")
}
regularClosureWithArgument{$0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment