ios - 对成员 Swift 3 的模糊引用

标签 ios oauth-2.0 swift2 swift3

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

关于ios - 对成员 Swift 3 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41952036/

相关文章:

java - LinkedIn OAuth2/oauth/v2/accessToken 响应未返回 "token_type"

typescript - 如何诊断passport.authenticate?

ios - 如何在 NS 字典中设置类对象

c - 如何将我的 swift 类转换为像 objective-c 中的 __bridge 这样的 UnsafePointer

iphone - 在圆上放置按钮

iphone - NSAutoreleasepool 泄漏 - 不明白为什么?

ios - 除了内存泄漏,什么会随着时间的推移减慢 iOS 应用程序的速度?

android - phonegap开发环境

带有用于 RESTful API 的社交网络的 Python OAuth2 服务器

xcode - Swift 中具有内部原始值的公共(public)枚举