需要澄清关于闭包/完成处理程序

时间:2022-01-15 17:09:32

I have been following a video tutorial, and have written the following code:

我一直在关注一个视频教程,并编写了以下代码:

func downloadWeatherDetails(completed: ()->() ) {

    let currentWeatherURL = URL(string: CURRENT_WEATHER_URL)!

    Alamofire
        .request(currentWeatherURL)
        .responseJSON(completionHandler: { response in
        let result = response.result
        print(result)
    })
    completed()
}

So basically, my understanding is as follows. The .responseJSON handler lets you call code after the request has been fired. It allows you to specify a completionHandler, which in my case, is the closure:

基本上,我的理解是这样的。. responsejson处理程序允许您在请求被触发后调用代码。它允许您指定一个completionHandler,在我的例子中,它是闭包:

{ response in
        let result = response.result
        print(result)
}

However, what I don't understand is what the "response" keyword actually signifies. I researched the usage of closures and saw that the syntax is:

然而,我不明白的是“响应”关键字到底意味着什么。我研究了闭包的用法,并发现语法是:

{(param) -> returnType in { code here }

{(param) -> returnType in {code here}

Thus, is the "response" keyword a parameter? If so, how is it being declared and where is the data coming from? How is the data passed into the "response" object? Also, why is only one parameter allowed? The code did not work if I made it as follows, for example:

因此,“response”关键字是一个参数吗?如果是,如何声明它,数据来自哪里?如何将数据传递到“response”对象?另外,为什么只允许一个参数?如果我按如下所示编写代码,则该代码不起作用,例如:

{ (response, test) in
        let result = response.result
        print(result)
}

I would really appreciate a thorough explanation on this as I've found no help elsewhere online. I've gone through Apple's "The Swift Programming Language", a multitude of different explanations, and similar questions, but still do not understand completely.

我真的很感激对此的详尽解释,因为我在网上找不到任何帮助。我读过苹果的“快速编程语言”,有很多不同的解释,也有类似的问题,但我还是不能完全理解。

Just to clarify, I do not believe my question is a duplicate since my question revolves primarily on the captured value stored in response rather than the syntax of closures as a whole. I went through the linked question while trying to figure out my own problem, but it did not help me sufficiently.

澄清一下,我不认为我的问题是重复的,因为我的问题主要关注的是响应中存储的捕获值,而不是闭包的整体语法。当我试图解决自己的问题时,我通过了这个链接的问题,但它并没有给我足够的帮助。

Minor clarification needed:

小需要澄清:

Is it always the case that when a method takes a closure as one of its parameters, for example, .testMethod(testParam: (String) -> ()) and would thus in practice be used: .testMethod(testParam: { (capturedVar) in statements} (correct me if im wrong), is it always the case that the parameter of the closure ((String) in this case) will be captured and stored in capturedVar? Will there always be data passed into the variable you define? Or is this cycle specific to alamofire?

它总是如此,当一个方法接受一个闭包作为它的一个参数,例如,.testMethod(testParam:(String)- >()),因此在实践中使用:.testMethod(testParam:{ }(capturedVar)语句(如果我错了,请纠正我),总是关闭的情况下,参数(在本例中(String))将被捕获并存储在capturedVar吗?是否总有数据被传递到您定义的变量中?或者这个循环是特定于alamofire的吗?

1 个解决方案

#1


1  

Swift closures are defined as:

Swift闭包定义为:

{ (parameters) -> return_type in
    statements
}

That is, the names in parenthesis are the variables the closure has captured, and the -> type is the optional return type (optional because the compiler can usually infer it). Alamofire's responseJSON method captures a DataResponse<Any> parameter, which you can name whatever you want, but which is usually just named response. You can then access it inside that closure.

也就是说,括号中的名称是闭包捕获的变量,而->类型是可选的返回类型(可选的,因为编译器通常可以推断它)。Alamofire的responseJSON方法捕获了一个DataResponse 参数,您可以命名任何您想要的名称,但是它通常只是命名响应。然后可以在这个闭包中访问它。

Also, your completed() call should be inside the responseJSON call, not outside, otherwise it just gets called immediately.

而且,您完成的()调用应该在responseJSON调用中,而不是在外部,否则它会立即被调用。

#1


1  

Swift closures are defined as:

Swift闭包定义为:

{ (parameters) -> return_type in
    statements
}

That is, the names in parenthesis are the variables the closure has captured, and the -> type is the optional return type (optional because the compiler can usually infer it). Alamofire's responseJSON method captures a DataResponse<Any> parameter, which you can name whatever you want, but which is usually just named response. You can then access it inside that closure.

也就是说,括号中的名称是闭包捕获的变量,而->类型是可选的返回类型(可选的,因为编译器通常可以推断它)。Alamofire的responseJSON方法捕获了一个DataResponse 参数,您可以命名任何您想要的名称,但是它通常只是命名响应。然后可以在这个闭包中访问它。

Also, your completed() call should be inside the responseJSON call, not outside, otherwise it just gets called immediately.

而且,您完成的()调用应该在responseJSON调用中,而不是在外部,否则它会立即被调用。