Swift 3.0迁移后的Alamofire错误:“ Call'中的额外参数”(请求方法) [英] Alamofire error after Swift 3.0 migration: “Extra argument in Call'” (request method)
问题描述
我目前正在将代码库更新为Swift 3.0,并且正在使用Alamofire。
因此,我不得不将Alamofire更新为4.0(Alamofire git repo)。
我有一个请求方法
使用Xcode的迁移工具后,我最终遇到以下错误: Call中的额外参数
。
我不太确定为什么此方法不再有效。
任何帮助都将是很棒的!
I am currently in the process of updating my codebase to Swift 3.0, and I am using Alamofire.
Thus, I had to update Alamofire to 4.0 (Alamofire git repo).
I have a request method to fetch data from the server, and before migration, it worked wonderfully.
After using Xcode's migration tool, I ended up with this error : "Extra argument in Call"
.
I'm not quite sure why this method is no longer working.
Any help would be wonderful!
class func makeRequest(
_ method: RequestMethod,
authorization: String?,
uri: String,
params: JSONDictionary?,
retry: Bool = true,
completionHandler: @escaping RequestCompletionBlock)
{
let requestURL = baseURL + uri
let authHeader: Dictionary<String, String>? = authorization != nil ? ["Authorization" : authorization!] : nil
//requestURL is highlighted, says "Extra argument in call"
sharedAlamofireManager.request(Method(rawValue: method.rawValue)!, requestURL, parameters: params, encoding: .JSON, headers: authHeader).responseJSON {
response in
}
}
编辑:JSONDictionary:
JSONDictionary:
typealias JSONDictionary = Dictionary<String, Any>
sharedAlamoFireManager也是SessionManager
sharedAlamoFireManager is also a SessionManager
推荐答案
每个AlamoFire docs RequestAdapter协议是Alamofire 4中的一项全新功能。它允许在创建之前对在SessionManager上进行的每个请求进行检查和调整。使用适配器的一种非常特定的方法是将Authorization标头附加到特定类型的身份验证后面的请求中。
Per AlamoFire docs The RequestAdapter protocol is a completely new feature in Alamofire 4. It allows each Request made on a SessionManager to be inspected and adapted before being created. One very specific way to use an adapter is to append an Authorization header to requests behind a certain type of authentication.
标头不再
首先,我必须创建AccessTokenAdapter类:
First I had to create the AccessTokenAdapter class:
class AccessTokenAdapter: RequestAdapter {
private let accessToken: String
init(accessToken: String) {
self.accessToken = accessToken
}
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
var urlRequest = urlRequest
if urlRequest.urlString.hasPrefix(RequestManager.returnBaseURL()) {
urlRequest.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization")
}
return urlRequest
}
}
然后我需要更新我的方法:
Then I needed to update my method:
class func makeRequest(
_ method: RequestMethod,
authorization: String?,
uri: String,
params: JSONDictionary?,
retry: Bool = true,
completionHandler: @escaping RequestCompletionBlock)
{
let requestURL = baseURL + uri
var accessToken: String?
if let authorization = authorization {
accessToken = authorization
}else{
accessToken = self.token()!
}
sharedAlamofireManager.adapter = AccessTokenAdapter(accessToken: accessToken!)
sharedAlamofireManager.request(requestURL, method: .get, parameters: params, encoding: JSONEncoding.default)
.responseJSON { response in
}
}
这篇关于Swift 3.0迁移后的Alamofire错误:“ Call'中的额外参数”(请求方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!