简体   繁体   English

Swift桥接方法调用错误

[英]Swift bridging method call bug

Objective-C method Objective-C方法

typedef void(^CompletionHandler)(NSError *error);
- (void)openWithCompletionHandler:(CompletionHandler)completionHandler authType:(AuthType)authType, ...;

I have tried to convert the object c code into swift code. 我试图将目标C代码转换为Swift代码。

I tried : 我试过了 :

test().openWithCompletionHandler({ (NSError) -> Void in
            }, AuthType.Test)

But this is code "Extra argument in call" compile error. 但这是代码“调用中的额外参数”编译错误。

What should I do? 我该怎么办?

Swift doesn't bridge Objective-C methods with variable arguments* ( ... ), so the `openWithCompletionHandler(:authType:) method you've declared isn't showing up at all in Swift. Swift不会将Objective-C方法与可变参数*( ... )桥接在一起,因此您声明的openWithCompletionHandler(:authType :)方法根本不会在Swift中显示。

From the error message you're getting, I can tell that test() is returning an AnyObject , which Swift is happy to call any Objective-C method on. 从收到的错误消息中,我可以看出test()返回的是AnyObject ,Swift很高兴可以调用任何Objective-C方法。 UIDocument has an openWithCompletionHandler() method that takes a single closure as its only argument, so Swift is complaining that you're giving it too many arguments for that method (even though it's not the one you want). UIDocument有一个openWithCompletionHandler()方法,该方法将单个闭包作为唯一参数,因此Swift抱怨说您为该方法提供了太多参数(即使它不是您想要的)。 Clear enough? 足够清楚吗?

If possible, you'll need to refactor the Objective-C method to something that Swift can understand. 如果可能的话,您需要将Objective-C方法重构为Swift可以理解的方法。


*Note that this isn't entirely true - the NSArray initWithObjects: method is bridged to a Swift initializer with a variadic parameter, but Apple appears to be doing something non-public to make that work - similar to how they're able to clarify whether arguments and return values should be optional or not. *请注意,这并非完全正确NSArray initWithObjects:方法通过可变参数连接到Swift初始化程序,但是Apple似乎在做一些非公开的事情来使它起作用-类似于它们能够澄清的方式参数和返回值是否应该是可选的。

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

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