func caculate(opr : String) ->(Int,Int) ->Int {
func add(a : Int,b : Int) -> Int {
return a + b
}
func sub(a : Int,b : Int) -> Int {
return a - b
}
var result : (Int,Int) -> Int
switch opr{
case "+":
result = add
case "-":
result = sub
default:
result = add
}
return result
}
let f1 :(Int,Int)->Int = caculate("+")
println(f1(1,2))
let f2 :(Int,Int)->Int = caculate("-")
println(f2(10,2))
原文链接:https://www.f2er.com/swift/327504.html