我是swift编程的新手。我已经按照其他教程编写了这段代码,但是我找不到正确的写入类型来执行操作?
@IBAction func operation(_ sender: UIButton) {
let operate = sender.currentTitle!
switch operate {
case "+" : performOperation() {}
case "-" : performOperation() {}
case "*" : performOperation() {}
case "/" : performOperation() {}
default: break
}
}
func performOperation(operate: (Double, Double) -> Double) {
}
答案 0 :(得分:1)
performOperation
方法接受(Double, Double) -> Double
类型的参数。
现在这个论点可以是以下任何一个:
方法-1。类型(Double, Double) -> Double
方法-2。签名为(Double, Double) -> Double
以下示例使用两种方法:
func operation()
{
let operate = sender.currentTitle!
switch operate
{
case "+" : performOperation(operate: add) //using Method-2
case "-" : performOperation(){(a: Double, b: Double) -> Double in
return a - b
}//using Method-1
default: break
}
}
func add(a: Double, b: Double) -> Double
{
return a + b
}
func performOperation(operate: (Double, Double) -> Double)
{
let x = operate(3, 4)
print(x)
}
同样,您可以对cases
switch statement.
使用这两种方法中的任何一种