简体   繁体   English

Swift中使用的Objective-C框架——方法错误的歧义使用

[英]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:我有一个 Objective-C 框架,其中我弃用了一种方法并将其替换为另一种方法:

+ (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:这在使用该框架的 Objective-C 项目中运行良好,但是当我尝试在 Swift 项目中调用该方法时,出现以下错误:

AppDelegate.swift:21:3: Ambiguous use of 'methodName' AppDelegate.swift:21:3: '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.问题似乎是方法名称自动转换为 Swift 与仅将第一个字母更改为小写有冲突。

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?有什么方法可以将已弃用的方法包装在 #if OBJECTIVE_C 类型编译指示中,以便 Swift 模块仅暴露于该方法的一个(较新)版本?

Or another way of working around the automatic translation?或者另一种解决自动翻译的方法?

Both Objective-C methods are imported to Swift as两种 Objective-C 方法都被导入到 Swift 中

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 :如果不推荐使用的方法在 Swift 中根本不可用,那么您可以使用NS_SWIFT_UNAVAILABLE对其进行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 :另一种选择是使用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以便将其导入 Swift 作为

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.想要查询更多的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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