David Seek David Seek - 1 month ago 12
Swift Question

Swift 3 / How to solve: "Ambiguous use of 'authorize(_:completion:)'"

Please don't close because of duplicate of "Ambiguous use of...". Even with intense research I was not able to find any similar threads solving my issue.

I'm updating a project to Swift 3 and am stuck at a compiler error:


Ambiguous use of 'authorize(_:completion:)'


Code:

func connectToInstagram() {

let auth: NSMutableDictionary = ["client_id": INSTAGRAM_CLIENT_ID,
SimpleAuthRedirectURIKey: INSTAGRAM_REDIRECT_URI]

SimpleAuth.configuration()["instagram"] = auth

SimpleAuth.authorize("instagram") { (anyObject, error) in // error here

if anyObject != nil {...


SimpleAuth is a Framework to handle Social Media authentication written in Objective C.

SimpleAuth:

open class SimpleAuth : NSObject {

open class func authorize(_ provider: String!, completion: SimpleAuth.SimpleAuthRequestHandler!)


enter image description here

SimpleAuthRequestHandler:

public typealias SimpleAuthRequestHandler = (Any?, Error?) -> Swift.Void

public let SimpleAuthPresentInterfaceBlockKey: String
public let SimpleAuthDismissInterfaceBlockKey: String


enter image description here

I have tried to change the line to:

_ = SimpleAuth.authorize("instagram") { (anyObject: Any?, error: Error?) in

_ = SimpleAuth.authorize("instagram", completion: { (anyObject: Any?, error: Error?) in


But just as expected, it didn't changed anything. What am I missing? Help is very appreciated.

Build Log:

enter image description here

xy/InstagramVC.swift:409:9: error: ambiguous use of 'authorize(_:completion:)'
SimpleAuth.authorize("instagram") { (any: Any?, error: Error?) -> Swift.Void in
^
SimpleAuth.SimpleAuth:24:21: note: found this candidate
open class func authorize(_ provider: String!, completion: SimpleAuth.SimpleAuthRequestHandler!)
^
SimpleAuth.SimpleAuth:34:21: note: found this candidate
open class func authorize(_ provider: String!, options: [AnyHashable : Any]! = [:], completion: SimpleAuth.SimpleAuthRequestHandler!)

par par
Answer

I think your problem is that you have parentheses around the closure arguments but no return type specified, so the compiler can't tell whether it's a single argument closure where the argument is a tuple with two fields, or if it's a two-argument closure (the first argument is Any?, thus the ambiguity).

Try doing this:

authorize("instagram") { object, error in ... }

or

authorize("instagram") { (object, error) -> Void in ... }