Swift 1.2 Not Working with Same Function Name and Different Parameter

Swift 1.2 not working with same function name and different parameter

You cannot overload methods that Objective-C can see, because Objective-C has no overloading:

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

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

Simple solution: hide these methods from Objective-C. For example, declare them both private.

More tricky solution: change the name by which Objective-C sees one of them. For example:

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 going so far as to make it private:

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

Swift: A function as an another functions argument

  1. It says condition: lessThanTen because in Swift you can define "labels", that is, descriptions for the data that a function call requires. It gives you an error because the call would be correct if the function declaration was func hasAnyMatches(list: [Int], #condition: Int -> Bool) -> Bool (at least this would be the case with Swift 1.2). The number sign produces, inside every call to the function, a label identical to the name of the function argument. Said label must not be deleted from the call, since it's there to clarify the purpose of that parameter to whoever invokes the function.
  2. list: [Int] means that the first function argument takes name "list" and is of type [Int]. condition: Int -> Bool means that the second function argument takes name "condition" and is of type "Int to Bool", that is, condition is a function that takes a single argument of type Int and returns a value of type Bool. In fact, if you read the last line, in the call to the function hasAnyMatches the following are passed: i) an array of Ints and ii) a function that takes an Int and returns a Bool (note that when passing functions as parameters we only write their name - no parentheses).

Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector

Objective-C does not support method overloading, you have to use a different method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand does support overloading, that's why it works when you remove the inheritance.

Weird behaviour on Swift protocol methods

As we know from Swift documentation for functions, foo and bar are external names for aString parameter. So technically he have exactly the same function with just different names of parameters that is completely legal in Objective-C:

id<MyProtocol> myVar;
[myVar myProtocolWithBar:@""];
[myVar myProtocolWithFoo:@""];

I think it is a bug in Swift compiler! File a report: http://bugreport.apple.com



Related Topics



Leave a reply



Submit