【发布时间】:2019-07-30 09:51:29
【问题描述】:
我正在编写一个函数来使用 AlamoFire 调用 POST 请求。我正在传递 URL 和参数。我需要返回 Alamofire 请求的响应。
这是我的代码:
func callAPI(params: Dictionary<String, Any>, url: String) -> Void {
let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
hud.contentColor = UIColor.red
DispatchQueue.global().async {
Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding(options: []), headers: nil).responseJSON { response in
DispatchQueue.main.async {
hud.hide(animated: true)
switch response.result{
case .success:
if let resultJson = response.result.value as? Dictionary<String,Any>{
print(resultJson)
// Success
}
case .failure(let error):
print(error)
//Failed
}
}
}
}
}
我想从这个函数返回响应字典 resultJson。我想为所有 API 调用重用这个函数。
如何重写此函数以获得解决方案?
【问题讨论】:
标签: ios swift alamofire swift4.2