简体   繁体   中英

Objective-C framework used in Swift - ambiguous use of method error

I have an Objective-C framework where I've deprecated a method and replaced it with another one:

+ (void)methodName:(BOOL)value; + (void)MethodName:(BOOL)value __attribute__((deprecated("from version 2.0 - use 'methodName'")));

Which works fine in an objective-C project using the framework, but when I try and call that method in a Swift project I get the following error:

AppDelegate.swift:21:3: Ambiguous use of 'methodName' 在此处输入图片说明

(double clicking the ! Found this candidate line doesn't go anywhere)

The problem appears to be that the automatic conversion to Swift of the method names has a conflict with just the first letter being changed to lower case.

Is there any way I can wrap the deprecated method in a #if OBJECTIVE_C type pragma so that the Swift module only gets exposed to the one (newer) version of the method?

Or another way of working around the automatic translation?

Both Objective-C methods are imported to Swift as

open class func methodName(_ value: Bool)

and that causes the ambiguity.

If the deprecated method should not be available in Swift at all then you can annotate it with NS_SWIFT_UNAVAILABLE :

+ (void)methodName:(BOOL)value;
+ (void)MethodName:(BOOL)value __attribute__((deprecated("from version 2.0 - use 'methodName'")))
    NS_SWIFT_UNAVAILABLE("deprecated method not available in Swift");

Another option is to annotate the deprecated method with NS_SWIFT_NAME :

+ (void)methodName:(BOOL)value;
+ (void)MethodName:(BOOL)value __attribute__((deprecated("from version 2.0 - use 'methodName'")))
    NS_SWIFT_NAME(oldMethodName(_:));

so that it is imported to Swift as

open class func methodName(_ value: Bool)

@available(*, deprecated, message: "from version 2.0 - use 'methodName'")
open class func oldMethodName(_ value: Bool)

See

for more information.

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