傳遞和返回功能
以下函式返回另一個函式作為其結果,稍後可以將其分配給變數並呼叫:
func jediTrainer () -> ((String, Int) -> String) {
func train(name: String, times: Int) -> (String) {
return "\(name) has been trained in the Force \(times) times"
}
return train
}
let train = jediTrainer()
train("Obi Wan", 3)