简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM