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
- 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 wasfunc 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. 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 typeInt
and returns a value of typeBool
. In fact, if you read the last line, in the call to the functionhasAnyMatches
the following are passed: i) an array ofInts
and ii) a function that takes anInt
and returns aBool
(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
How to Calculate the Energy Per Bin in a Dft
How to Use Tabs to Evenly Space Out Description Strings in Swift
How to Segue Values When My Viewcontroller Is Embedded in an Uinavigationcontroller
Deep Copy of Cmimagebuffer or Cvimagebuffer
Sending Firebase Push Notifications to Logged-In Users Only
Uisegment Value Changing When Tableview Get Scrolled
Adding Elements on Many-To-Many Relation
Swiftui - Make Toolbar's Navigationlink Use Detail View
Swift For-In Loop with Enumerate on Custom Array2D Class
How to Move Platform with Velocity
Swift Firestore Search for Users
Skscene Becomes Unresponsive While Being Idle
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files
How to Use Spritekit Archives with Skspritenode Subclasses
Swift 2.2, Contains Method Not Working
Swift Protocol with Variadic Property
Dyld: Library Not Loaded, App Requires Afnetworking 2.0.0 But Provides Version 1.0.0