Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 3中创建类似Alamofire的自定义闭包_Ios_Iphone_Swift_Swift3_Closures - Fatal编程技术网

Ios 如何在swift 3中创建类似Alamofire的自定义闭包

Ios 如何在swift 3中创建类似Alamofire的自定义闭包,ios,iphone,swift,swift3,closures,Ios,Iphone,Swift,Swift3,Closures,我是这样结束的: static func Test (printUrl: String, OnCompleted: @escaping (_ respons: String) -> Void) { OnCompleted (printUrl) } ClassNameFile.Test(printUrl: "hi") { (respons) in print(respons) <#code#> } 我可以这样定义一个响应: stat

我是这样结束的:

static func Test (printUrl: String, OnCompleted: @escaping (_ respons:     String) -> Void) {
         OnCompleted (printUrl)
}
 ClassNameFile.Test(printUrl: "hi") { (respons) in

    print(respons)
   <#code#>
}
我可以这样定义一个响应:

static func Test (printUrl: String, OnCompleted: @escaping (_ respons:     String) -> Void) {
         OnCompleted (printUrl)
}
 ClassNameFile.Test(printUrl: "hi") { (respons) in

    print(respons)
   <#code#>
}
您可以看到它定义了一些其他项,如请求、响应、数据和结果。如何为自己的封口制作这些物品

我的另一个问题是关于“请求”和“响应”! 这些东西是什么?是延期还是其他事情


求你了。举个例子?

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
    }
}
公共结构数据响应{
///发送到服务器的URL请求。
公共let请求:URLRequest?
///服务器对URL请求的响应。
公共let响应:HTTPURLResponse?
///服务器返回的数据。
公共数据:数据?
///响应序列化的结果。
公开结果:结果
///请求的整个生命周期的时间线。
公共租赁时间表:时间表
///如果结果成功,则返回结果的关联值,否则返回'nil'。
公共变量值:值?{return result.value}
///如果结果失败,则返回关联的错误值,否则返回'nil'。
公共变量错误:错误?{return result.error}
变量度量:任何对象?
///使用从响应序列化派生的指定参数创建“DataResponse”实例。
///
///-参数请求:发送到服务器的URL请求。
///-参数响应:服务器对URL请求的响应。
///-参数数据:服务器返回的数据。
///-参数结果:响应序列化的结果。
///-参数timeline:“请求”的完整生命周期的时间线。默认为“timeline()”。
///
///-返回:新的“DataResponse”实例。
公共初始化(
请求:URL请求?,
响应:HTTPURLResponse?,
数据:数据?,
结果:结果,,
时间线:时间线=时间线()
{
self.request=请求
自我反应
self.data=数据
self.result=结果
self.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)
    }
public func responseObject(队列:DispatchQueue?=nil,键路径:String?=nil,mapToObject对象:T?=nil,上下文:MapContext?=nil,completionHandler:@escaping(DataResponse)->Void)->Self{
返回响应(队列:队列,响应序列化程序:DataRequest.ObjectMapperSerializer(键路径,mapToObject:object,上下文:context),completionHandler:completionHandler)
}

如果您想了解更多详细信息,请访问Github页面,共

有人吗?回答我?你说的其他事情都在回答中。在闭包中可以有多个项,就像有响应一样。请给我一个简单的例子,我真的是初学者