【问题标题】:Ambiguous reference to member Swift 3对成员 Swift 3 的模糊引用
【发布时间】:2017-01-31 07:35:05
【问题描述】:

我正在将我的项目从 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

错误状态

对成员'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
    }

所以请指导我谢谢。

【问题讨论】:

    标签: ios oauth-2.0 swift2 swift3


    【解决方案1】:

    我认为这个问题是由于 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)
     }
    

    【讨论】:

    【解决方案2】:

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

    let fileManager = FileManager()
    

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

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

    【讨论】:

      【解决方案3】:

      我在将其从 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)
                                      }
      
                })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多