[英]Swift extension method for Alamofire to return SwiftyJSON result
I migrated an app from Swift 2.2 to 3.0 which used an extension method from the Alamofire-SwiftyJSON project on GitHub. 我将应用程序从Swift 2.2迁移到了3.0,该应用程序使用了GitHub上Alamofire-SwiftyJSON项目的扩展方法。 Alamofire-SwiftyJSON allows receiving the response from an Alamofire network request converted to a SwiftyJSON instance like this:
Alamofire-SwiftyJSON允许接收来自Alamofire网络请求的响应,该请求已转换为SwiftyJSON实例,如下所示:
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.responseSwiftyJSON({ (request, response, json, error) in
print(json) // json is a SwiftyJSON 'JSON' instance
print(error)
})
The Alamofire-SwiftyJSON project was not updated for Swift 3 as of writing this question. 撰写此问题时,尚未为Swift 3更新Alamofire-SwiftyJSON项目。 I'm looking for an equivalent implementation of the
responseSwiftyJSON
extension method that works with Swift 3+ and Alamofire 4+. 我正在寻找与Swift 3+和Alamofire 4+一起使用的
responseSwiftyJSON
扩展方法的等效实现。
This solution incorporates a suggestion for working with Alamofire from the SwiftyJSON readme . 该解决方案结合了SwiftyJSON自述文件中有关使用Alamofire的建议。
It is based on similar extensions included with Alamofire in ResponseSerialization.swift : 它基于ResponseSerialization.swift中 Alamofire包含的类似扩展:
DataRequest.responseJSON(queue:options:completionHandler:)
DataRequest.jsonResponseSerializer(options:)
This solution works with Swift 3 and above. 此解决方案适用于Swift 3及更高版本。 It was tested with Alamofire 4.2+ and SwiftyJSON 3.1.3+.
已在Alamofire 4.2+和SwiftyJSON 3.1.3+上进行了测试。
import Alamofire
import SwiftyJSON
extension DataRequest {
/// Adds a handler to be called once the request has finished.
///
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
/// - parameter completionHandler: A closure to be executed once the request has finished.
///
/// - returns: The request.
@discardableResult
public func responseSwiftyJSON(
queue: DispatchQueue? = nil,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (DataResponse<JSON>) -> Void) -> Self {
return response(
queue: queue,
responseSerializer: DataRequest.swiftyJSONResponseSerializer(options: options),
completionHandler: completionHandler
)
}
/// Creates a response serializer that returns a SwiftyJSON instance result type constructed from the response data using
/// `JSONSerialization` with the specified reading options.
///
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
///
/// - returns: A SwiftyJSON response serializer.
public static func swiftyJSONResponseSerializer(
options: JSONSerialization.ReadingOptions = .allowFragments) -> DataResponseSerializer<JSON> {
return DataResponseSerializer { _, response, data, error in
let result = Request.serializeResponseJSON(options: options, response: response, data: data, error: error)
switch result {
case .success(let value):
return .success(JSON(value))
case .failure(let error):
return .failure(error)
}
}
}
}
Example Use: 使用示例:
Alamofire.request("https://httpbin.org/get").validate().responseSwiftyJSON {
response in
print("Response: \(response)")
switch response.result {
case .success(let json):
// Use SwiftyJSON instance
print("JSON: \(json)")
case .failure(let error):
// Handle error
print("Error: \(error)")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.