Swift Alamofire 4 在没有自定义请求适配器的情况下向请求添加标头

问题描述 投票:0回答:1

我正在尝试迁移到新的 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;
                        }
                    }
            }*/
ios swift alamofire
1个回答
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;
            }
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.