对成员 Swift 3 的引用不明确

Ambiguous reference to member Swift 3

我正在将我的项目从 Swift 2.3 迁移到 Swift 3。并且遇到了预期的困难。

这是一个用于 OAuth 的函数,使用 OAuthSwift。我试过转换

class func OAuthSwiftAuthorization(inViewController viewController: UIViewController, withOAuthInfo info:FitnessTracker, successHandler:@escaping MyOAuthNewSuccessHandler, failure: @escaping ((_ error: NSError) -> Void)) {

    let oauthswift = OAuth2Swift(
        consumerKey:    info.consumerKey,
        consumerSecret: info.consumerSecret,
        authorizeUrl:   info.authorizeUrl,
        accessTokenUrl: info.accessTokenUrl,
        responseType:   info.responseType
    )

    oauthswift.authorizeURLHandler = SafariURLHandler(viewController: viewController, oauthSwift: oauthswift)
    oauthswift.accessTokenBasicAuthentification = true
    oauthswift.allowMissingStateCheck = true

    oauthswift.authorize(withCallbackURL: URL(string: info.callBackUrl)!, scope: info.scope, state: info.state, success: { (credential, response, parameters) in

             successHandler(credential, response, parameters)
    }) { (error) in

        failure(error: error)
        print(error.localizedDescription)
    }
}

但我在

处收到错误消息
oauthswift.authorize(withCallbackURL: URL(string: info.callBackUrl)!, scope: info.scope, state: info.state, success: { (credential, response, parameters) in

错误状态

Ambiguous reference to member 'authorize(withCallbackURL:scope:state:parameters:headers:success:failure:)'

这是 Swift 2.

中的工作代码
    oauthswift.authorizeWithCallbackURL(
        URL(string: info.callBackUrl)!,
        scope: info.scope, state:info.state,
        success: { credential, response, parameters in

            successHandler(credientials: credential, response: response, params: parameters)
        },
        failure: { error in

            failure(error: error)
            print(error.localizedDescription)
        }
    )

更新:

错误没有出现,直到我输入成功和失败的交易者。这很好:

        oauthswift.authorize(withCallbackURL: URL(string: info.callBackUrl)!, scope: info.scope, state: info.state, success: { (credential, response, parameters) in
        // successHandler(credential, response, parameters)
    }) { (erorr) in
        // failure(error: error
    }

所以请指导我谢谢。

我认为问题是Swift结合闭包的类型推断的一些不足造成的。 您可以尝试以下方法之一:

要么不使用尾随闭包,例如

oauthswift.authorize(withCallbackURL: URL(string: info.callBackUrl)!, scope: info.scope, state: info.state, success: { (credential, response, parameters) in

         successHandler(credential, response, parameters)
}, failure: { (error) in

    failure(error: error)
    print(error.localizedDescription)
})

或为错误提供明确的类型,例如

 oauthswift.authorize(withCallbackURL: URL(string: info.callBackUrl)!, scope: info.scope, state: info.state, success: { (credential, response, parameters) in

         successHandler(credential, response, parameters)
 }) { (error: Error) in

     failure(error: error)
     print(error.localizedDescription)
 }

供参考: 当有多个variable/method同名时出现这种错误,你的oauthswift是否有多个"thing"叫"authorize"?喜欢另一种方法? 我的错误是我声明:

let fileManager = FileManager()

并在

let _ = try localFileManager.createDirectory(...) 

我遇到了同样的错误,更改 localFileManager 中的变量名称修复了它。

我遇到了同样的错误对成员的模糊引用,使用相同的方法将其从Swift 4 转换为Swift 5 。看起来完成处理程序已更改为支持新的 Result 类型。将完成处理程序更改为以下解决了我的问题,

        oauthVarSwift.authorize( withCallbackURL: URL(string: "")!,
                             scope: "", state:"", completionHandler: {  result in
                                switch result {
                                case .success(let credential, let response,  let parameters):
                                    print(credential.oauthToken)

                                case .failure(let error):
                                 print(error)
                                }

          })