Swift- Closures & Tag Closures learning Notes

We learned the basic syntax of closures, but today we’ll learn a little more advanced stuff

Closure shorthand

1. Normal trailing closure writing and calling logic

 func travel(action: (String) - >String) {

  print("I'm getting ready to go")

  let desc = action("Beijing ")

  print(desc)

  print("I'm arrived! ")}Copy the code
travel { (place: String) - >String in

  return "1. I'm going to \(place) in my car"

}
Copy the code
  1. Because Swift can infer that the closure argument type is String, it can be simply shortened to place

travel { place -> String in

  return "2. I'm going to \(place) in my car"

 }
Copy the code
  1. In further Swift also knows to return a string, so we can put-> StringDelete it:
 travel { place in 

  return "3. I'm going to \(place) in my car"

 }
Copy the code
  1. Because the closure function has only one line of code that must return a value, Swift allows us to remove itreturnKey words:
travel { place in

  "4. I'm going to \(place) in my car"

 }
Copy the code
  1. You can even omit place in. Swift provides a syntax for closure parameters to automatically get parameters, + parameter ordinal: ‘palce=+ parameter ordinal:’ palce=0 ‘

 travel { 

  "5. I'm going to \ [$0) in my card"

 }
Copy the code

Abbreviations before and after

 //before
 travel { (place: String) - >String in

  return "1. I'm going to \(place) in my car"

 }
 
 //end
 travel { 

  "5. I'm going to \ [$0) in my card"

 }
Copy the code

Closure surfaces accept multiple arguments and return values (String, Int) -> String

 func travel(action: (String.Int) - >String) {

  print("I'm getting ready to go.")

  let description = action("London".60)

  print(description)

  print("I arrived!")

 }
 
  travel {

  "6. I'm going to ($0) at ($1) miles per hour."

 }

Copy the code

Closure value capture

 func travel(a)- > (String) - >Void {

  return {
    print("I'm going to \ [$0)")}}let result = travel2()

 result("London ,Beijing")

 func travel2(a)- > (String) - >Void {

  var counter = 1

  return {

    print("\(counter). I'm going to \ [$0)")

    counter + = 1}}Copy the code
 result("London")

 result("London")

 result("London")

2. I'm going to London
3. I'm going to London
4. I'm going to London
Copy the code

Closures can capture defined constants and variables from the context, even if the original scope in which they were defined no longer exists, and the closure can reference and modify those values in its function body

Closures are reference types

In Swift, functions and closures are reference types and whenever you assign a function or closure to a constant or variable, you’re actually setting constants and variables to be references to functions and closures, right

test

Using what we learned today, let’s simplify the following function calls and discuss them in the comments section

func getDirections(to destination: String.then travel: ((String]) -> Void) {
	let directions = [
		"Go straight ahead"."Turn left onto Station Road"."Turn right onto High Street"."You have arrived at \(destination)"
	]
	travel(directions)
}

// Normal call
getDirections(to: "London") { (directions: [String]) in
	print("I'm getting my car.")
	for direction in directions {
		print(direction)
	}
}
Copy the code

To simplify the getDirections call

Put your code in the comments sectionCopy the code

To summarize the closure learning

  1. Closure functions can be assigned to variables and can be called directly later.
  2. Closures can take arguments and return values, just like regular functions.
  3. Closures can be passed to functions as arguments, and closures can have their own arguments and return values.
  4. If the function ends with closure arguments, you can use trailing closure syntax.
  5. Swift automatically provides parameter shorthand syntax, multiple parameters: 0,0,0,1
  6. Closures can capture externally defined constants and variables. Closures are reference types

reference

www.hackingwithswift.com/100