簡體   English   中英

如何在Swift 3中創建像Alamofire這樣的自定義閉包

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

我做了這樣的關閉:

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”的! 這些是什么? 擴展或其他任何東西?

請 。 舉個例子?

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頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM