简体   繁体   中英

Swift bridging method call bug

Objective-C method

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

I have tried to convert the object c code into swift code.

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.

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. 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). Clear enough?

If possible, you'll need to refactor the Objective-C method to something that Swift can understand.


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

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