【发布时间】:2015-06-29 08:19:25
【问题描述】:
我正在学习 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
我的代码有什么问题?
【问题讨论】:
-
你的类是 NSObject 的子类吗?
-
@flashadvanced 类定义如下: class ViewController: UIViewController { .... } 我猜 UIViewerController 继承自 UIResponder : NSObject
标签: swift functional-programming