我正在学习Swift,我正在学习Paul Hegarty关于如何使用波兰语反向表示法构建计算器的教程。代码如下所示:
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsEnteringData{
enter()
}
switch operation {
case "×": performOperation {$0 * $1}
case "÷": performOperation {$1 / $0}
case "+": performOperation {$0 + $1}
case "−": performOperation {$1 - $0}
case "√": performOperation {sqrt($0)}
case "sin": performOperation {sin($0)}
case "cos": performOperation {cos($0)}
case "π": performOperation{$0 * M_PI}
default: break
}
}
func performOperation (operation : ( Double, Double ) -> Double){
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
func performOperation (operation : Double -> Double){
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
xCode编译器不喜欢performOperation函数的第二个实例,并报告它之前已经定义过。它报告的错误是:
Method 'performOperation' with Objective-C selector 'performOperation:' conflicts with previous declaration with the same Objective-C selector
我的代码出了什么问题?
答案 0 :(得分:5)
我不知道您的类声明,但在这种情况下最可能的问题是您从@objc
类继承。 Objective-C不支持方法重载,但Swift确实如此。因此,如果可以,您或者不应该继承自@objc
类,或者您可以使用不同的方法名称。
供参考,您可以阅读this post