【问题标题】:how create custom closure like Alamofire in swift 3如何在 swift 3 中创建像 Alamofire 这样的自定义闭包
【发布时间】:2017-03-10 10:09:10
【问题描述】:

我做了一个这样的闭包:

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)")
 }
}

您可以看到它定义了一些其他项目,例如请求、响应、数据、结果。我怎样才能为自己的闭包制作这些物品?

我的另一个问题是关于“request”和“responseJSON”的! 这些物品是什么?扩展程序或其他任何东西?

请。举个例子?

【问题讨论】:

  • 有人吗?回答我?
  • 您所说的其他内容在响应中。就像您有响应一样,您可以在闭包中包含多个项目。

标签: ios iphone swift swift3 closures


【解决方案1】:

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 页面

【讨论】:

  • 请给我一个简化的例子,我真的是初学者
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 2017-08-15
  • 1970-01-01
  • 2018-07-12
相关资源
最近更新 更多