2

In Swift 3.0, the automated changing of function names due to the "Omit Needless Words" rule has caused two functions in an ObjC class to be the same.

- (void)showLoader;

...and...

- (void)show __deprecated_msg("User 'showLoader'");

The problem is that these functions are within a third party Cocoa Pod (otherwise I would just delete the unnecessary 'show' function).

This results in getting the error "Ambiguous use of 'show'" when I try to invoke the function like this:

loader?.show()

Is there a way to reverse the automatic changing of function name in Swift 3.0 or to help the compiler know which function I want to invoke?

Thanks for your help!

PJeremyMalouf
  • 613
  • 6
  • 15

2 Answers2

2

See MartinR's answer to my similar question here: Converting to Swift 3 renamed my own Objective-C method

If you owned the code, you could use NS_SWIFT_NAME(showLoader()) after your method declaration to force the ObjC-to-Swift method conversion to be named what you want:

- (void)showLoader NS_SWIFT_NAME(showLoader());

I think it's worth mentioning even though in your case it doesn't exactly solve your problem because you don't own the code.

Community
  • 1
  • 1
Ethan G
  • 1,353
  • 2
  • 15
  • 31
  • I did not know this macro, works like a charm ! should be the accepted answer in my opinion ^^ thanks Ethan – polo987 Apr 20 '17 at 13:07
1

You can work around this by calling

loader?.perform(Selector("showLoader"))

You will see a warning from the compiler, but it will compile successfully, and things will work correctly at runtime.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    And file a bug report with Apple, please. Unfortunately the "renamification" train has left the station. In my opinion they didn't give this "feature" anywhere near enough thought before loosing it upon the world... – matt Oct 03 '16 at 16:48
  • 1
    And file an issue with your third party cocoapod, or, even better, go fix it and send them a PR. – i_am_jorf Oct 03 '16 at 16:58
  • 1
    Very good point, @i_am_jorf, but it is hardly the fault of some innocent Objective-C coder that Swift now idiotically thinks that two perfectly distinct Objective-C methods are one and the same. – matt Oct 03 '16 at 17:11
  • It's not their fault, but either way their library won't work. – i_am_jorf Oct 03 '16 at 18:18
  • 1
    @i_am_jorf Not so. It is _Swift_ that turns `showLoader` into `show`. There's nothing wrong with this code in Objective-C. There is no ambiguity whatever. Just because some other language might like to call into this library, and just because that other language is stumbling over its own ill-advised feet, doesn't mean that the Objective-C coder is doing anything wrong or owes anything to the user of that other language. If I were the author of this library, I'd tell someone who filed an issue to buzz off. The issue is with Swift, not with the library. – matt Oct 03 '16 at 18:25
  • Thanks for the solution! I wrote out the invokation like this to and I didn't get any warnings: _ = loader?.perform(#selector(PQFLoaderProtocol.showLoader)) – PJeremyMalouf Oct 04 '16 at 00:56