Swift Alamofire 4在没有自定义请求适配器

时间:2016-10-25 19:14:30

标签: ios swift alamofire

我正在尝试迁移到新的alamofire,并且在文档中它告诉我我需要做一个自定义请求适配器以便向请求添加标头,我认为这并不舒适无处不在,我想只是添加请求中的标题,而不是必须为某些请求做出更低的价格,任何人都找到了办法吗?

下面我分享了我的旧代码(注释)和我的新代码,它缺少一个标题参数。

            Alamofire.request(url, method: .post , parameters: parameters, encoding: JSONEncoding.default)
                .responseJSON { response in
                    if response.result.isSuccess {
                        let time = DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
                        DispatchQueue.main.asyncAfter(deadline: time) {
                            self.loadAreasFromServer();
                            self.busy = false;
                        }
                    }
            }

            /*Alamofire.request(.POST, url,headers:headers,parameters:parameters,
                encoding: .json)
                .responseJSON { response in
                    if response.result.isSuccess {
                        let time = DispatchTime(DispatchTime.now()) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
                        DispatchQueue.main.asyncAfter(deadline: time) {
                            self.loadAreasFromServer();
                            self.busy = false;
                        }
                    }
            }*/

1 个答案:

答案 0 :(得分:3)

方法签名已更改。这应该适合你:

Alamofire.request(url, method: .post , parameters: parameters, encoding: JSONEncoding.default, headers: headers)
    .responseJSON { response in
        if response.result.isSuccess {
            let time = DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: time) {
                self.loadAreasFromServer();
                self.busy = false;
            }
        }
}