ios - 如何在 Alamofire 4 和 Swift 3 中设置方法、标题、参数
<p><p>在过去版本的<code>Alamofire</code>中,对于发送方法、 header 和参数,我曾经这样做过:</p>
<pre><code>Alamofire.request(.GET, URLRequest, headers:headers, parameters: parameters)
</code></pre>
<p>但是版本 4 和 swift 3 是不同的。
如何设置方法、发送 header 和参数?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>迁移指南位于 <a href="https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md" rel="noreferrer noopener nofollow">Alamofire github</a>很好地解释了这一点。</p>
<p>看这里:</p>
<pre><code>// Alamofire 3
let parameters: = ["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("Progress: \(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)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 Alamofire 4 和 Swift 3 中设置方法、标题、参数,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39877112/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39877112/
</a>
</p>
页:
[1]