简体   繁体   中英

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"! 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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