简体   繁体   English

如何在Swift 3中创建像Alamofire这样的自定义闭包

[英]how create custom closure like Alamofire in swift 3

I made a closure like this: 我做了这样的关闭:

static func Test (printUrl: String, OnCompleted: @escaping (_ respons:     String) -> Void) {
         OnCompleted (printUrl)
}

I can define a response like this: 我可以这样定义一个响应:

 ClassNameFile.Test(printUrl: "hi") { (respons) in

    print(respons)
   <#code#>
}

It's fine but see codes below: 很好,但是请参见下面的代码:

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

You can see it define some other items like request, response, data, result. 您可以看到它定义了其他一些项目,例如请求,响应,数据,结果。 How can i make these items for my own closures? 如何为自己的瓶盖制作这些物品?

My another question is about "request" and "responseJSON"! 我的另一个问题是关于“ request”和“ responseJSON”的! What are these items? 这些是什么? An extension or any other things? 扩展或其他任何东西?

Please . 请 。 give an example? 举个例子?

Response in Alamofire is an object which has request, data, result, response as it's members. Alamofire中的响应是一个具有请求,数据,结果和响应的对象。 So you can access it via . 因此,您可以通过访问它. , while in your case it's just a string. ,而在您的情况下,它只是一个字符串。 So you need to pass a object instead of String. 因此,您需要传递一个对象而不是字符串。

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

This is how the method definition looks like 这就是方法定义的样子

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

If you want to get more details go to Github page of Alamofire 如果您想了解更多详细信息,请访问Alamofire的 Github页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM