0

I am new to swift and have an error in the following code. I have one function with two different parameters. Xcode(version 6) is giving an error on the second definition with a parameter as a function which takes one value.Here is the code:

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

}
sanjiv soni
  • 77
  • 1
  • 7

1 Answers1

1

Update:
Another solution is add private before method definition (Source):

private 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()
    }

}

Original Answer:
Looks like your methods are defined in class that is inherited from some Objective-C class, for example:

class TestClass : NSObject {  
    func test(a : String) {}
    func test(a : UInt) {}
}

Compiler will produce such error:

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

To fix that you need avoid inheritance from Objective-C class:

class TestClass {  
    func test(a : String) {}
    func test(a : UInt) {}
}

This variant will work correct.

The problem is that Objective-C doesn't support methods overloading, but Swift does. That's why you need create pure Swift class.

Community
  • 1
  • 1
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57