问题描述
- 【swift初学者求助】关于找不到某重载的错误提示
-
刚开始学swifit,跟着斯坦福大学公开课学的
做一个计算器
代码如下:import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel! var userIsInTheMiddleOfTypingANumber: Bool = false var operandStack = [Double]() var displayValue: Double{ get { return NSNumberFormatter().numberFromString(display.text!)!.doubleValue } set { display.text = "(newValue)" userIsInTheMiddleOfTypingANumber = false } } func performOperation(operation: (Double, Double) -> Double) { if operandStack.count >= 2 { displayValue = operation(operandStack.removeLast(), operandStack.removeLast()) enter() } } private func performOperation(operation: Double -> Double) { if operandStack.count >= 1 { displayValue = operation(operandStack.removeLast()) enter() } } @IBAction func appendDigit(sender: UIButton) { let digit = sender.currentTitle! if userIsInTheMiddleOfTypingANumber { display.text = display.text! + digit } else { display.text = digit userIsInTheMiddleOfTypingANumber = true } } @IBAction func enter() { userIsInTheMiddleOfTypingANumber = false operandStack.append(displayValue) println("operandStack = (operandStack)") } @IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userIsInTheMiddleOfTypingANumber { enter() } switch operation { case "×":
// performOperation {$0 * $1}
performOperation(*)
case "÷":
performOperation {$1 / $0}
case "+":
// performOperation {$0 + $1}
performOperation(+)
case "?":
performOperation {$0 - $1}
case "√":
performOperation(sqrt)
default: break
}
}
}实现乘法计算的时候
这么写没有错误提示:
performOperation(*)而实现加法计算的时候
同样的写法提示错误
performOperation(+)错误内容:
Could not find an overload for '+' that accepts the supplied arguments弄不明白
乘法和加法都是二元运算
为什么乘法这么写没有错误
加法却有错误呢
望大神指教
谢谢!
解决方案
http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara
时间那么久了,你应该也解决了吧,上面是类似的问题。不能重载但却重载了的原因。
我也是新手,多交流学习。