在过去版本的Alamofire 中,对于发送方法、 header 和参数,我曾经这样做过:
Alamofire.request(.GET, URLRequest, headers:headers, parameters: parameters)
但是版本 4 和 swift 3 是不同的。
如何设置方法、发送 header 和参数?
Best Answer-推荐答案 strong>
迁移指南位于 Alamofire github很好地解释了这一点。
看这里:
// Alamofire 3
let parameters: [String: AnyObject] = ["foo": "bar"]
Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print("Bytes: \(bytesRead), Total Bytes: \(totalBytesRead), Total Bytes Expected: \(totalBytesExpectedToRead)")
}
.validate { request, response in
// Custom evaluation closure (no access to server data)
return .success
}
.responseJSON { response in
debugPrint(response)
}
// Alamofire 4
let parameters: Parameters = ["foo": "bar"]
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default)
.downloadProgress(queue: DispatchQueue.utility) { progress in
print("rogress: \(progress.fractionCompleted)")
}
.validate { request, response, data in
// Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
}
关于ios - 如何在 Alamofire 4 和 Swift 3 中设置方法、标题、参数,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39877112/
|