Swift 3.0 迁移后的 Alamofire 错误:“调用中的额外参数”(请求方法)

Posted

技术标签:

【中文标题】Swift 3.0 迁移后的 Alamofire 错误:“调用中的额外参数”(请求方法)【英文标题】:Alamofire error after Swift 3.0 migration: “Extra argument in Call'” (request method) 【发布时间】:2016-10-05 16:31:34 【问题描述】:

我目前正在将我的代码库更新到 Swift 3.0,并且我正在使用 Alamofire。因此,我必须将 Alamofire 更新到 4.0(Alamofire git repo)。我有一个请求方法来从服务器获取数据,在迁移之前,它工作得很好。使用 Xcode 的迁移工具后,我遇到了这个错误:"Extra argument in Call"。我不太清楚为什么这个方法不再有效.任何帮助都会很棒!

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

编辑:JSONDictionary:

typealias JSONDictionary = Dictionary<String, Any>

sharedAlamoFireManager 也是一个 SessionManager

【问题讨论】:

【参考方案1】:

根据 AlamoFire 文档 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.

header 不再是请求方法的参数,而是设置在请求适配器内部。

首先我必须创建 AccessTokenAdapter 类:

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
    

然后我需要更新我的方法:

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

    

【讨论】:

您好,我正在尝试遵循文档并收到编译错误:***.com/questions/44687745/…

以上是关于Swift 3.0 迁移后的 Alamofire 错误:“调用中的额外参数”(请求方法)的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire 自定义响应从 Alamofire v1.3 迁移到 3.0(和 Swift 2 语法)

Alamofire Swift 3.0 调用中的额外参数

在 Swift 3.0 (Alamofire 4.4.0) 中的一些请求后,Alamofire 停止工作

Swift 3.0 迁移后的 Project-Swift.h 文件编译相同函数数百次,编译耗时 7 分钟

Alamofire 的 Swift 扩展方法返回 SwiftyJSON 结果

POST 请求 Swift 3.0 Alamofire