Ambiguous reference to member when working with ObjC protocols

""Hi,


consider this ObjC class (.m file is not relevant, so not shown)


@interface Bar: NSObject
- (void)barWithString:(NSString *)string;
- (void)barWithInteger:(NSInteger)integer;
@end


If from a Swift file I try to create this selector


let selector = #selector((Bar.bar) as (Bar) -> (String) -> (Void))


the compiler does not complain. The casting to that specific signature is resolved and life is good.


Now considering this simple ObjC protocol


@protocol Doable
- (void)doSomethingWithString:(NSString *)string;
- (void)doSomethingWithInteger:(NSInteger)integer;
@end


I am trying to create a selector the same way


let selector = #selector((Doable.doSomething) as (Doable) -> (String) -> (Void))


but this time the compiler hits me with "Ambiguous reference to member 'doSomething(with:)'"


Can someone explain why this different behavior?

Thanks


Best

.pm

Replies

There’s some other factor in play here. I put your code into a new test project and it worked for me. Specifically:

  1. Using Xcode 9.0 I created a new command line tool project in Swift.

  2. I created a new Objective-C class and put your

    Bar
    interface in the header (and filled out dummy implementation methods to make the compiler happy).
  3. I created a new header and put the

    Doable
    protocol in that.
  4. I included both headers in the bridging header.

  5. I set main.swift to this:

    import Foundation
    
    
    let selectorB = #selector((Bar.bar) as (Bar) -> (String) -> (Void))
    let selectorD = #selector((Doable.doSomething) as (Doable) -> (String) -> (Void))
    print(selectorB)
    print(selectorD)

    -

  6. It compiled and ran just fine, printing this:

    barWithString:
    doSomethingWithString:

    -

Perhaps you can repeat the steps above to see what you get.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"