Umair Ahmed
Umair Ahmed

Reputation: 73

Ambiguous use of 'initialize()'

I am using third-party library which is implemented on objective c. I have to develop application in swift.

The library code which I have to use is 'initialize'

@interface VidyoClientConnector : NSObject
{}

    +(BOOL) Initialize;
    +(BOOL) SetExperimentalOptions:(const char*)options;
    +(void) Uninitialize;
@end

But when using the above's Initialize method I get ambiguous use of

From swift:

VidyoClientConnector.initialize()

Exception

ViewController.swift:17:9: Ambiguous use of 'initialize()'

Upvotes: 2

Views: 558

Answers (1)

Martin R
Martin R

Reputation: 539685

Both

+[VidyoClientConnector Initialize]

and

+[NSObject initialize]

are imported to Swift as initialize() class method, and that causes the ambiguity. Renaming the Objective-C method would be the best option (but perhaps not possible since it is not your framework).

If you have write access to the headers then you can define a different name for Swift:

+(BOOL) Initialize NS_SWIFT_NAME(vidyoInit());

which can then be used as

let result = VidyoClientConnector.vidyoInit()

If you cannot modify the headers then you can implement a wrapper method in an Objective-C category:

// .h file:
@interface VidyoClientConnector (Wrapper)
+(BOOL) vidyoInit;
@end

// .m file:
@implementation VidyoClientConnector (Wrapper)
+(BOOL) vidyoInit { return [self Initialize]; }
@end

Finally, in this particular case, you can use that the two methods have different return values, and resolve the ambiguity with

let result = (VidyoClientConnector.initialize as () -> Bool)()

without changing any Objective-C code.

Upvotes: 2

Related Questions