0

I am learning 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 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 selector conflicts with previous declaration with the same 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
    }
}
}
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
shibboleth
  • 21
  • 4
  • yah surely i will not allow you or even other language won't allow such thing that you use same name for two different things. complier will be confuse if it allows to call which function where. – Fatti Khan May 18 '15 at 05:10
  • @FattiKhan: Not true. Many languages support function overloading where you have functions of the same name differentiated by argument signature. I believe Swift does, too. – Thilo May 18 '15 at 05:14
  • @Thilo http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara – Fatti Khan May 18 '15 at 05:16
  • performOperation(operation: Double -> Double) Here you are passing return type within parameter so put return type outside the braces – Fatti Khan May 18 '15 at 05:18
  • (If you search for that error message on SO then you'll find at least 5 questions about exactly the same problem in exactly the same code from the same Stanford course :) – Martin R May 18 '15 at 05:21
  • @MartinR Good Call - Didn't occur to me at all – shibboleth May 18 '15 at 13:46

1 Answers1

1

Change this:

func performOperation(#operationWithOneParametr: Double -> Double)

for this:

func performOperation(#operationWithTwoParameters: (Double) -> Double)

All Closures must be like: (parameters) -> return type

For more detail about Closures have a look in apple documentation: The Swift Programming Language - Closures

Also the functions have identical signatures that is not support by Swift, to fix it declare those functions as follow:

func performOperation(#operationWithTwoNumber: (Double, Double) -> Double) {
    if operandStack.count >= 2 {
        displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
        enter()
    }
}

func performOperation(#operationWithOneNumber: (Double) -> Double) {
    if operandStack.count >= 1 {
        displayValue = operation(operandStack.removeLast())
        enter()
    }
}

The "#" forces the first parameter to be part of the signature what make the two signature difference.

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • found the answer here: http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara – shibboleth May 18 '15 at 13:50
  • @shibboleth, sorry I just notice that this two have the same signature, I edit the answer to help you with that too. – Icaro May 18 '15 at 21:47