user426132
user426132

Reputation: 1441

Method with a completion block that can be used from objective-c

I need to call a swift function from objective-c. This needs to have a callback (that will be executed from obj-c)

@objc public func getTokens(forAuthentificationCode auth_code : String, completion: ()->()? )

I get "Method cannot be marked @objc because the parameter..." How to make a function with a completion block that can be used from objective-c?

Upvotes: 2

Views: 1128

Answers (1)

Cristik
Cristik

Reputation: 32772

You declared the completion parameter as ()->()?, which translates to a closure with no parameters and that returns a Void?. Objective-C supports optionals only for class arguments, thus the completion block is not Objective-C compatible.

I think however that you intended to declare the whole closure as optional, in order to allow callers to pass nil, in that case you need to wrap the closure definition into another set of parentheses: completion: (() -> Void)?. I changed the return type from () to Void for better readability, as Void is just an alias for ().

Upvotes: 3

Related Questions