Ambiguous reference when using selectors

Ready example for testing on playgrounds:

import Foundation

class Demo {

    let selector = #selector(someFunc(_:))

    func someFunc(_ word: String) {
        // Do something
    }

    func someFunc(_ number: Int) {
        // Do something
    }

}

I get an error Ambiguous use of 'someFunc'.

I tried this answer and got something like:

let selector = #selector(someFunc(_:) as (String) -> Void)

But still get the same result.

Any clues on how to solve this?

+4
source share
2 answers

Short answer: this is not possible.

Long explanation: if you want to use #selector, the methods must be subjective Objective-C, so your code will look like this:

@objc func someFunc(_ word: String) {
    // Do something
}

@objc func someFunc(_ number: Int) {
    // Do something
}

Thanks to the annotation, @objcthey are now exposed to Objective-C and a consistent tone is created. Objective-C can use it to call Swift methods.

Objective-C , objc_msgSend: , , , . :

'someFunc' Objective-C selector 'someFunc:' Objective-C.

- , .

+2

, . , :

@objc func someFunc(word: String) {
    // Do something
}

@objc func someFunc(number: Int) {
    // Do something
}

:

#selector(someFunc(word:))

#selector(someFunc(number:))
+1

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


All Articles