let driving = {
print("I'm driving in my car")
}
driving()
Accepting parameters in a closure
let driving = { (place: String) in
print("I'm going to \(place) in my car")
}
driving("London")
Returning values from a closure
let drivingWithReturn = { (place: String) -> String in
return "I'm going to \(place) in my car"
}
let message = drivingWithReturn("London")
print(message)
Closures as parameters
let driving = {
print("I'm driving in my car")
}
func travel(action: () -> Void) {
print("I'm getting ready to go.")
action()
print("I arrived!")
}
travel(action: driving)
Trailing closure syntax
func travel(action: () -> Void) {
print("I'm getting ready to go.")
action()
print("I arrived!")
}
travel() {
print("I'm driving in my car")
}
travel {
print("I'm driving in my car")
}
Using closures as parameters when they accept parameters
func travel(action: (String) -> Void) {
print("I'm getting ready to go.")
action("London")
print("I arrived!")
}
travel { (place: String) in
print("I'm going to \(place) in my car")
}
Using closures as parameters when they return values
func travel(action: (String) -> String) {
print("I'm getting ready to go.")
let description = action("London")
print(description)
print("I arrived!")
}
travel { (place: String) -> String in
return "I'm going to \(place) in my car"
}
Shorthand parameter names
func travel(action: (String) -> String) {
print("I'm getting ready to go.")
let description = action("London")
print(description)
print("I arrived!")
}
travel { (place: String) -> String in
return "I'm going to \(place) in my car"
}
travel { place -> String in
return "I'm going to \(place) in my car"
}
travel { place in
return "I'm going to \(place) in my car"
}
travel { place in
"I'm going to \(place) in my car"
}
travel {
"I'm going to \($0) in my car"
}
Closures with multiple parameters
func travel(action: (String, Int) -> String) {
print("I'm getting ready to go.")
let description = action("London", 60)
print(description)
print("I arrived!")
}
travel {
"I'm going to \($0) at \($1) miles per hour."
}
Returning closures from functions
func travel() -> (String) -> Void {
return {
print("I'm going to \($0)")
}
}
let result = travel()
result("London")
let result2 = travel()("London")
Capturing values
func travel() -> (String) -> Void {
return {
print("I'm going to \($0)")
}
}
let result = travel()
result("London")
func travel() -> (String) -> Void {
var counter = 1
return {
print("\(counter). I'm going to \($0)")
counter += 1
}
}
result("London")
result("London")
result("London")
You’ve made it to the end of the sixth part of this series, so let’s summarize:
You can assign closures to variables, then call them later on.Closures can accept parameters and return values, like regular functions.You can pass closures into functions as parameters, and those closures can have parameters of their own and a return value.If the last parameter to your function is a closure, you can use trailing closure syntax.Swift automatically provides shorthand parameter names like $0 and $1, but not everyone uses them.If you use external values inside your closures, they will be captured so the closure can refer to them later.欢迎分享,转载请注明来源:内存溢出
评论列表(0条)