I am learning swift and I am trying to build a calculator. I am trying to create the square root function. I have already created multiply, add, subtract and divide and have a function called performOperation with two doubles. I know in Swift, I should be a able to create another function titled performOperation with a single double and swift is smart enough to use the correct function for the square root function, but I am getting an error at the second func performOperation line that says "method 'performOperation' with objective-c selector conflicts with previous declaration with the same objective-c selector." What am I doing wrong??
Code below:
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var display: UILabel!
var userIsTyping = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsTyping {
display.text = display.text! + digit
} else {
display.text = digit
userIsTyping = true
}
}
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsTyping {
enter()
}
switch operation {
case "×": performOperation { $0 * $1 }
case "÷": performOperation { $1 / $0 }
case "+": performOperation { $0 * $1 }
case "−": performOperation { $1 - $0 }
case "√": performOperation { sqrt($0) }
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()
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsTyping = false
operandStack.append(displayValue)
println("operandStack = \(operandStack)")
}
//Changes String Value to a Double
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set {
display.text = "\(newValue)"
userIsTyping = false
}
}
}