Reputation: 5
i am new in programming swift.i have made this code by following other tutorials but i can´t find the correct write typ to perform operation?
@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) {
}
Upvotes: 0
Views: 1487
Reputation: 24341
performOperation
method accepts an argument of type (Double, Double) -> Double
.
Now this argument can be any of the below:
Method-1. A closure of type (Double, Double) -> Double
Method-2. A method name having signature as (Double, Double) -> Double
The below example uses both the methods:
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)
}
Similarly, you can use any of the 2 methods for all other cases
of switch statement.
Upvotes: 1