我做了这样的封闭:
static func Test (printUrl: String, OnCompleted: @escaping (_ respons: String) -> Void) {
OnCompleted (printUrl)
}
我可以定义这样的响应:
ClassNameFile.Test(printUrl: "hi") { (respons) in
print(respons)
<#code#>
}
很好,但请参阅下面的代码:
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
您可以看到它定义了其他一些项目,例如请求,响应,数据,结果。我如何为自己的关闭做这些物品?
我的另一个问题是关于"请求"one_answers" Responsejson"!这些物品是什么?扩展名或其他任何东西?
请。举一个例子?
Alamofire
中的响应是一个对象,它具有请求,数据,结果,响应作为成员。因此,您可以通过.
访问它,而在您的情况下只是一个字符串。因此,您需要通过对象而不是字符串。
public struct DataResponse<Value> {
/// The URL request sent to the server.
public let request: URLRequest?
/// The server's response to the URL request.
public let response: HTTPURLResponse?
/// The data returned by the server.
public let data: Data?
/// The result of response serialization.
public let result: Result<Value>
/// The timeline of the complete lifecycle of the request.
public let timeline: Timeline
/// Returns the associated value of the result if it is a success, `nil` otherwise.
public var value: Value? { return result.value }
/// Returns the associated error value if the result if it is a failure, `nil` otherwise.
public var error: Error? { return result.error }
var _metrics: AnyObject?
/// Creates a `DataResponse` instance with the specified parameters derived from response serialization.
///
/// - parameter request: The URL request sent to the server.
/// - parameter response: The server's response to the URL request.
/// - parameter data: The data returned by the server.
/// - parameter result: The result of response serialization.
/// - parameter timeline: The timeline of the complete lifecycle of the `Request`. Defaults to `Timeline()`.
///
/// - returns: The new `DataResponse` instance.
public init(
request: URLRequest?,
response: HTTPURLResponse?,
data: Data?,
result: Result<Value>,
timeline: Timeline = Timeline())
{
self.request = request
self.response = response
self.data = data
self.result = result
self.timeline = timeline
}
}
这就是方法定义的样子
public func responseObject<T: BaseMappable>(queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, context: MapContext? = nil, completionHandler: @escaping (DataResponse<T>) -> Void) -> Self {
return response(queue: queue, responseSerializer: DataRequest.ObjectMapperSerializer(keyPath, mapToObject: object, context: context), completionHandler: completionHandler)
}
如果您想获得更多详细信息,请访问alamofire的GitHub页面