Fixing ambiguous error when Swift calls Objective-C method: Ambiguous use of 'add'

Description

In a Swift project, RCCoreClient’s methods are simplified into the add method, causing an error during the call:

- (void)addConnectionStatusChangeDelegate:(id<RCConnectionStatusChangeDelegate>)delegate;
- (void)addReceiveMessageDelegate:(id<RCIMClientReceiveMessageDelegate>)delegate;

Analysis

This issue occurs due to method simplification in Objective-C when used with Swift versions below Swift 4. It’s resolved in Swift 4 and later versions.

Solution

Add an Objective-C adapter class in the Swift project to implement the methods. Use the adapter’s methods in the Swift project.

+ (void)addConnectionStatusChangeDelegate:(id<RCConnectionStatusChangeDelegate>)delegate {
    [[RCCoreClient sharedCoreClient] addConnectionStatusChangeDelegate:delegate];
}

+ (void)addReceiveMessageDelegate:(id<RCIMClientReceiveMessageDelegate>)delegate {
    [[RCCoreClient sharedCoreClient] addReceiveMessageDelegate:delegate];
}

Example call:

func test() {
    OCAdapter.addReceiveMessageDelegate(self)
    OCAdapter.addConnectionStatusChangeDelegate(self)
}

Links

https://stackoverflow.com/questions/35658334/how-do-i-resolve-ambiguous-use-of-compile-error-with-swift-selector-syntax

More support

If you have questions, feel free to submit a ticket.