Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CLOSURES - PART TWO
- */
- // CLOSURES AS PARAMETERS WHEN THEY ACCEPT PARAMETERS
- func travel(action: (String) -> Void) {
- print("I'm getting ready to go")
- action("London")
- print("I have arrived!")
- }
- travel { (place: String) in
- print("I'm going to \(place) in my car.")
- }
- // CLOSURES AS PARAMETERS WHEN THEY RETURN VALUES
- func travel(action: (String) -> String) {
- print("I'm getting read to go")
- let description = action("Montreal")
- print(description)
- print("I've arrived!")
- }
- travel { (place: String) -> String in
- return "I'm going to \(place) in my car"
- }
- // SHORTHAND PARAMETER NAMES
- // Swift knows that the above parameter (place: String)
- // is a string. So it can be removed:
- travel { place -> String in
- return "I'm going to \(place) in my car"
- }
- // Swift also knows the return type must be a string
- travel { place in
- return "I'm going to \(place) in my car"
- }
- // Since there is only one line of code after "in"
- // "return" can be removed.
- travel {
- place in
- "I'm going to \(place) in my car"
- }
- // "place in" can be replaced by automatic names for closure parameters:
- travel {
- "I'm going to \($0) un my car"
- }
- // CLOSURES WITH MULTIPLE PARAMETERS
- // The following function has a closure as a parameter
- // that itself accepts two parameters:
- func travel(action: (String, Int) -> String) {
- print("I'm getting ready to go.")
- let description = action("London", 60)
- print(description)
- print("I arrived!")
- }
- // Full syntax call:
- travel { (location: String, speed: Int) -> String in
- return "I'm going to \(location) at \(speed) miles per hour."
- }
- // Swift knows the type of the parameters, so they can be removed
- travel { location, speed -> String in
- return "I'm going to \(location) at \(speed) miles per hour."
- }
- // Swift knows the return type, so it can be removed:
- travel { location, speed in
- return "I'm going to \(location) at \(speed) miles per hour."
- }
- // Since there is only one line of code after "in," "return" can be removed:
- travel { location, speed in
- return "I'm going to \(location) at \(speed) miles per hour."
- }
- // "location, speed in" can be replaced by automatic parameters:
- travel {
- "I'm going to \($0) at \($1) miles per hour."
- }
- // RETURNING CLOSURES FROM FUNCTIONS
- // The following function will return a closure
- // that is called by a string
- // but (the closure) returns void
- func travel() -> (String) -> Void {
- var counter = 1
- return {
- print("\(counter). I'm going to \($0)")
- counter += 1
- }
- }
- // Call function to get closure
- // Then call that closure with string "LA"
- let result = travel()
- result("LA") // counter = 1
- // CAPTURING VALUES
- // "counter" variable was created within travel()
- // and is captured by the closure each time and
- // will remain alive for that closure.
- result("London") // counter = 2
- result("Montreal") // counter = 3
Advertisement
Add Comment
Please, Sign In to add comment