Alamofire 5 类型 'Request' 没有成员 'authorizationHeader'
Posted
技术标签:
【中文标题】Alamofire 5 类型 \'Request\' 没有成员 \'authorizationHeader\'【英文标题】:Alamofire 5 type 'Request' has no member 'authorizationHeader'Alamofire 5 类型 'Request' 没有成员 'authorizationHeader' 【发布时间】:2020-03-25 17:37:13 【问题描述】:更新到 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
【问题讨论】:
您使用的是哪个版本的 Alamofire? 捆绑版本字符串(短)5.0.5 您所说的“不工作”是什么意思,您是否有任何错误,或者您希望某些东西没有出现?如果其中任何一个,您可以发布错误或您期望的内容吗? 更新问题请查看@denis_lor 【参考方案1】:以前在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)")
由于 Alamofire 5 中的 this commit 我们可以在 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")
]
【讨论】:
以上是关于Alamofire 5 类型 'Request' 没有成员 'authorizationHeader'的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 4.0 上的“类型‘任何’没有下标成员”错误
Alamofire+Combine:如何从 AFError 中获取自定义错误类型
Swift 5.1 和 Alamofire 5.1:GET 方法错误