https://www.youtube.com/watch?v=Cb0sy_H0LfU
https://code.tutsplus.com/tutorials/swift-from-scratch-function-parameters-types-and-nesting--cms-23056
6. Function Types
Functions as Parameters
In the previous article, we briefly touched upon function types. A function has a particular type, composed of the function's parameter types and its return type. The
printMessage(_:) function, for example, is of type (String) -> (). Remember that () symbolizes Void, which is equivalent to an empty tuple.
Because every function has a type, it's possible to define a function that accepts another function as a parameter. The following example shows how this works.
01
02
03
04
05
06
07
08
09
10
11
| func printMessage(_ message: String) { print(message)}func printMessage(_ message: String, with function: (String) -> ()) { function(message)}let myMessage = "Hello, world!"printMessage(myMessage, with: printMessage) |
The
printMessage(_:with:) function accepts a string as its first parameter and a function of type (String) -> () as its second parameter. In the function's body, the function that we pass in is invoked with the message argument.
The example also illustrates how we can invoke the
printMessage(_:with:) function. The myMessageconstant is passed in as the first argument and the printMessage(_:) function as the second argument. How cool is that?Functions as Return Types
It is also possible to return a function from a function. The next example is a bit contrived, but it illustrates what the syntax looks like.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
| func compute(_ addition: Bool) -> (Int, Int) -> Int { func add(_ a: Int, _ b: Int) -> Int { return a + b } func subtract(_ a: Int, _ b: Int) -> Int { return a - b } if addition { return add } else { return subtract }}let computeFunction = compute(true)let result = computeFunction(1, 2)print(result) |
The
compute(_:) function accepts a boolean and returns a function of type (Int, Int) -> Int. The compute(_:) function contains two nested functions that are also of type (Int, Int) -> Int, add(_:_:)and subtract(_:_:).
The
compute(_:) function returns a reference to either the add(_:_:) or the subtract(_:_:) function, based on the value of the addition parameter.
The example also shows how to use the
compute(_:) function. We store a reference to the function that is returned by the compute(_:) function in the computeFunction constant. We then invoke the function stored in computeFunction, passing in 1 and 2, store the result in the result constant, and print the value of result in the standard output. The example may look complex, but it is actually easy to understand if you know what is going on.func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")
留言
張貼留言