问题
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
}
}
Migration Guide for Alamofire4.0
Edit: JSONDictionary:
typealias JSONDictionary = Dictionary<String, Any>
sharedAlamoFireManager is also a SessionManager
回答1:
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.
The header is no longer a parameter of the request method, but instead set inside the request adapter.
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
}
}
来源:https://stackoverflow.com/questions/39879355/alamofire-error-after-swift-3-0-migration-extra-argument-in-call-request-me