GregLeck

Closures - Part II

Sep 30th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.95 KB | None | 0 0
  1. /*
  2.  CLOSURES - PART TWO
  3.  */
  4.  
  5. // CLOSURES AS PARAMETERS WHEN THEY ACCEPT PARAMETERS
  6. func travel(action: (String) -> Void) {
  7.     print("I'm getting ready to go")
  8.     action("London")
  9.     print("I have arrived!")
  10. }
  11.  
  12. travel { (place: String) in
  13.     print("I'm going to \(place) in my car.")
  14. }
  15.  
  16. // CLOSURES AS PARAMETERS WHEN THEY RETURN VALUES
  17. func travel(action: (String) -> String) {
  18.     print("I'm getting read to go")
  19.     let description = action("Montreal")
  20.     print(description)
  21.     print("I've arrived!")
  22. }
  23.  
  24. travel { (place: String) -> String in
  25.     return "I'm going to \(place) in my car"
  26. }
  27.  
  28. // SHORTHAND PARAMETER NAMES
  29.  
  30. // Swift knows that the above parameter (place: String)
  31. // is a string. So it can be removed:
  32. travel { place -> String in
  33.     return "I'm going to \(place) in my car"
  34. }
  35.  
  36. // Swift also knows the return type must be a string
  37. travel { place in
  38.     return "I'm going to \(place) in my car"
  39. }
  40.  
  41. // Since there is only one line of code after "in"
  42. // "return" can be removed.
  43. travel {
  44.     place in
  45.     "I'm going to \(place) in my car"
  46. }
  47.  
  48. // "place in" can be replaced by automatic names for closure parameters:
  49. travel {
  50.     "I'm going to \($0) un my car"
  51. }
  52.  
  53. // CLOSURES WITH MULTIPLE PARAMETERS
  54. // The following function has a closure as a parameter
  55. // that itself accepts two parameters:
  56. func travel(action: (String, Int) -> String) {
  57.     print("I'm getting ready to go.")
  58.     let description = action("London", 60)
  59.     print(description)
  60.     print("I arrived!")
  61. }
  62.  
  63. // Full syntax call:
  64. travel { (location: String, speed: Int) -> String in
  65.     return "I'm going to \(location) at \(speed) miles per hour."
  66. }
  67.  
  68. // Swift knows the type of the parameters, so they can be removed
  69. travel { location, speed -> String in
  70.     return "I'm going to \(location) at \(speed) miles per hour."
  71. }
  72.  
  73. // Swift knows the return type, so it can be removed:
  74. travel { location, speed in
  75.     return "I'm going to \(location) at \(speed) miles per hour."
  76. }
  77.  
  78. // Since there is only one line of code after "in," "return" can be removed:
  79. travel { location, speed in
  80.     return "I'm going to \(location) at \(speed) miles per hour."
  81. }
  82.  
  83. // "location, speed in" can be replaced by automatic parameters:
  84. travel {
  85.     "I'm going to \($0) at \($1) miles per hour."
  86. }
  87.  
  88. // RETURNING CLOSURES FROM FUNCTIONS
  89. // The following function will return a closure
  90. // that is called by a string
  91. // but (the closure) returns void
  92. func travel() -> (String) -> Void {
  93.     var counter = 1
  94.     return {
  95.         print("\(counter). I'm going to \($0)")
  96.         counter += 1
  97.     }
  98. }
  99.  
  100. // Call function to get closure
  101. // Then call that closure with string "LA"
  102. let result = travel()
  103. result("LA")    // counter = 1
  104.  
  105. // CAPTURING VALUES
  106. // "counter" variable was created within travel()
  107. // and is captured by the closure each time and
  108. // will remain alive for that closure.
  109. result("London")    // counter = 2
  110. result("Montreal")  // counter = 3
Advertisement
Add Comment
Please, Sign In to add comment