Umair Ahmed Umair Ahmed - 1 year ago 145
iOS Question

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()'

Answer Source

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 special 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download