【问题标题】:Alamofire error after Swift 3.0 migration: “Extra argument in Call'” (request method)Swift 3.0 迁移后的 Alamofire 错误:“调用中的额外参数”(请求方法)
【发布时间】: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

【问题讨论】:

    标签: ios swift3 alamofire


    【解决方案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
    
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2017-11-26
    • 2017-06-26
    • 2017-01-22
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多