1

I am learning Swift and I am following a tutorial from Paul Hegarty on how to build a calculator using Polish Inverse Notation. The code looks like this:

@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()

    }
}

The xCode compiler does not like the second instance of the performOperation function and reports that it has already been defined previously. The error it reports is:

Method 'performOperation' with Objective-C selector 'performOperation:' conflicts with previous declaration with the same Objective-C selector

What is wrong with my code?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
SharpRock
  • 11
  • 3

1 Answers1

5

I am not aware of your class declaration but the most possible issue in this case is that you inherit from an @objc class. Objective-C does not support method overloading but Swift does. So either you shouldn't be inheriting from am @objc class if you can or you can just use different method names.

For reference you can read this post

Community
  • 1
  • 1
Vasil Garov
  • 4,851
  • 1
  • 26
  • 37
  • Thank you for the pointing me to the reference. After reading the post I better understand the issue and possible fix. The route of the problem is that my class sort of breaks the MVC pattern as I have mixed "view" and "model". By splitting the "model" code into a separate class which does not inherit from UIControl I should be able to have two versions of the performOperation function one ( Double, Double) -> Double and a Double -> Double. – SharpRock Apr 22 '15 at 19:40
  • If my post answered your question you can accept it by clicking the tick. – Vasil Garov Apr 23 '15 at 07:51