Alamofire 5类型“Request”没有成员“authorizationHeader”

Sab*_*med 3 ios swift alamofire

更新到 Alamofire 5 后,“Request.authorizationHeader(用户:字符串,密码:字符串)”方法显示错误。

Error:- Type 'Request' has no member 'authorizationHeader'
Run Code Online (Sandbox Code Playgroud)

代码:

    // Safely unwrap token
    guard let safeHeader = Request.authorizationHeader(user: consumerKey!, password: consumerSecret!) else {
        return nil
    }
Run Code Online (Sandbox Code Playgroud)

den*_*lor 5

以前在下面的内容Request.authorizationHeader(..)现在在下面HTTPHeaders.authorization(..),为了更好地解释它,我将在这里放置它如何更改的代码:

在此提交之前,我们在 Request.swift 中有:

/// Returns a base64 encoded basic authentication credential as an authorization header tuple.
///
/// - parameter user:     The user.
/// - parameter password: The password.
///
/// - returns: A tuple with Authorization header and credential value if encoding succeeds, `nil` otherwise.
open class func authorizationHeader(user: String, password: String) -> (key: String, value: String)? {
    guard let data = "\(user):\(password)".data(using: .utf8) else { return nil }
    let credential = data.base64EncodedString(options: [])
    return (key: "Authorization", value: "Basic \(credential)")
}
Run Code Online (Sandbox Code Playgroud)

由于Alamofire 5中的此提交,我们可以在以下位置找到它:HTTPHeaders.swift

/// Returns a `Basic` `Authorization` header using the `username` and `password` provided.
///
/// - Parameters:
///   - username: The username of the header.
///   - password: The password of the header.
///
/// - Returns:    The header.
public static func authorization(username: String, password: String) -> HTTPHeader {
    let credential = Data("\(username):\(password)".utf8).base64EncodedString()

    return authorization("Basic \(credential)")
}
Run Code Online (Sandbox Code Playgroud)

这意味着现在您应该能够通过执行以下操作来完成相同的操作:

let headers: HTTPHeaders = [
        .authorization(username: consumerKey!, password: consumerSecret!),
        .accept("application/json")
    ]
Run Code Online (Sandbox Code Playgroud)