ios 对成员 Swift 3 的模棱两可的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41952036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 11:06:59  来源:igfitidea点击:

Ambiguous reference to member Swift 3

iosoauth-2.0swift2swift3

提问by Umair Afzal

I am migrating my project from Swift 2.3 to Swift 3. And having difficulty as expected.

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

Here is a function which is being used for OAuth, using OAuthSwift. I have tried to convert

这是一个用于 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)
    }
}

But I am getting an error at

但我在

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

Error states

错误状态

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

对成员 'authorize(withCallbackURL:scope:state:parameters:headers:success:failure:)' 的不明确引用

Here is the working code from Swift 2.

这是 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)
        }
    )

UPDATE:

更新:

Error does not appear unitil I type success and faliure handelrs. This complies fine:

错误不会出现,直到我输入成功和失败的handelrs。这很好:

        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
    }

So Please guide me Thanks.

所以请指导我谢谢。

回答by Andreas Oetjen

I think the problem is caused by some shortcomings of Swift's type inference in combination with closures. You could try one of the following:

我认为这个问题是由于 Swift 的类型推断结合闭包的一些缺点造成的。您可以尝试以下方法之一:

Either don't use trailing closures, e.g.

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

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)
})

or provide an explicit type for error, e.g.

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

 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)
 }

回答by Kappe

For reference: This kind of error appears when there's more than one variable/method with the same name, does your oauthswifthas more than one "thing" called "authorize"? like another method? My error was that i declared:

供参考:当有多个同名的变量/方法时会出现这种错误,您oauthswift是否有多个名为“授权”的“东西”?喜欢另一种方法?我的错误是我声明:

let fileManager = FileManager()

and in

并在

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

I got the same error, changing the variable name in localFileManagerfixed it.

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

回答by yaali

I got the same error Ambiguous reference to memberwith the same method on converting it from Swift 4 to Swift 5. Looks like the completion handler has been changed to support the new Resulttype. Changing the completing handler to below fixed the issue for me,

在将其从Swift 4转换为 Swift 5 时,我得到了同样的错误Ambiguous reference to member使用相同的方法。看起来完成处理程序已更改为支持新的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)
                                }

          })