Swift 1.2 does not work with the same function name and another parameter

I have 2 functions with the same name but with different parameters.

The first takes as a parameter a function that takes 2 doubles and returns one.

The second takes as a parameter a function that takes 1 double and returns it. This works in Swift 1.1, tested on Xcode 6.1.1, however in Swift 1.2, tested on Xcode 6.4 (beta), this does not work and gives me this error:

The 'performOperation' method with Objective-C selector 'performOperation:' conflicts with a previous declaration with the same Objective-C selector

What can I do to make this work, and why is this happening? I know that I can make the square root differently than here, but I want to know what the problem is.

<i> Change

@IBAction func operate(sender: UIButton) { let operation = sender.currentTitle! if userIsInMiddleOfTypingANumber{ 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() } } 
+6
source share
1 answer

You cannot overload the methods that Objective-C can see, since Objective-C has no overload:

 func performOperation(operation : (Double,Double) -> Double){ } func performOperation(operation : Double -> Double) { } 

(The fact that this was allowed in Swift before Swift 1.2 is actually a bug, Swift 1.2 closed the loophole and fixed the bug.)

A simple solution: hide these methods from Objective-C. For example, declare them as private .

A more complicated solution: change the name with which Objective-C sees one of them. For instance:

 func performOperation(operation : (Double,Double) -> Double){ } @objc(performOperation2:) func performOperation(operation : Double -> Double) { } 

Or, new in Swift 2.0, you can hide one or both of them from Objective-C without closing it:

 @nonobjc func performOperation(operation : (Double,Double) -> Double){ } func performOperation(operation : Double -> Double) { } 
+9
source

Source: https://habr.com/ru/post/988625/


All Articles