更新为Alamofire 5后,“Request.authorizationHeader(user:String,password:String)”方法显示错误。

Error:- Type 'Request' has no member 'authorizationHeader'

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

最佳答案

现在,过去在Request.authorizationHeader(..)下的内容现在在HTTPHeaders.authorization(..)下,为了更好地解释它,我将在此处放置代码更改的方式:

this commit之前,我们在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)")
}

由于this commit 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)")
}

这意味着现在您应该可以执行以下操作:
let headers: HTTPHeaders = [
        .authorization(username: consumerKey!, password: consumerSecret!),
        .accept("application/json")
    ]

关于ios - Alamofire 5类型的“请求”没有成员“authorizationHeader”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60854292/

10-13 03:59