我的API类中有以下两种方法可从API获取数据:
I have these two methods in my API class to get data from an API:
func authenticateUser(completionHandler: (responseObject: String?, error: NSError?) -> ()) { makeAuthenticateUserCall(completionHandler) } func makeAuthenticateUserCall(completionHandler: (responseObject: String?, error: NSError?) -> ()) { Alamofire.request(.GET, loginUrlString) .authenticate(user: "a", password: "b") .responseString { request, response, responseString, responseError in completionHandler(responseObject: responseString as String!, error: responseError) } }然后在另一个类中,我使用以下代码访问数据:
Then in another class i use the following code to access the data:
API().authenticateUser{ (responseObject, error) in println(responseObject) }代码正在工作,但我不明白
The code is working but i don't understand it completely.
推荐答案
completionHandler 是闭包参数。正如Swift文档所说:
completionHandler is a closure parameter. As Swift documentation says:
闭包是自包含的功能块,可以在代码中传递和使用。 Swift中的闭包类似于C和Objective-C中的块以及其他编程语言中的lambda。
Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
所以,闭包是什么
在您的情况下,您调用 authenticateUser ,然后传递一个接受(responseObject,错误)并执行 println(responseObject)。 authenticateUser()在 completionHandler 参数下收到您的关闭,然后调用 makeAuthenticateUserCall()将您的 completionHandler 闭包传递给它。
In your case, you call authenticateUser and you pass a closure that receives (responseObject, error) and executes println(responseObject). authenticateUser() receives your closure under the completionHandler parameter and it then calls makeAuthenticateUserCall() passing your completionHandler closure to it.
然后再次查看定义,您可以看到 func makeAuthenticateUserCall(completionHandler:(responseObject:String ?, error:NSError?)-> ()),这意味着像 authenticateUser() makeAuthenticateUserCall()一样,以 completionHandler 的名称接收一个闭包作为参数。 makeAuthenticateUserCall()使用 AlamoFire 发出网络请求,并再次捕获闭包下的响应,并将其作为 responseString()方法。因此,您具有:
Then again, looking at the definition you can see func makeAuthenticateUserCall(completionHandler: (responseObject: String?, error: NSError?) -> ()) that means that like authenticateUser() makeAuthenticateUserCall() is a function that receives a closure as a parameter, under the name of completionHandler. makeAuthenticateUserCall() makes a network request using AlamoFire and you capture the response under a closure again that you pass as parameter of the responseString() method. So you have:
//here you call authenticateUser with a closure that prints responseObject API().authenticateUser{ (responseObject, error) in println(responseObject) }然后:
//authenticateUser receives your closure as a parameter func authenticateUser(completionHandler: (responseObject: String?, error: NSError?) -> ()) { //it passes your closure to makeAuthenticateUserCall makeAuthenticateUserCall(completionHandler) } //makeAuthenticateUserCall receives your closure func makeAuthenticateUserCall(completionHandler: (responseObject: String?, error: NSError?) -> ()) { Alamofire.request(.GET, loginUrlString) .authenticate(user: "a", password: "b") //here you pass a new closure to the responseString method .responseString { request, response, responseString, responseError in //in this closure body you call your completionHandler closure with the //parameters passed by responseString and your code gets executed //(that in your case just prints the responseObject) completionHandler(responseObject: responseString as String!, error: responseError) } }有关更多信息,请阅读文档: Swift闭包
For more information read the documentation: Swift Closures
更多推荐
了解快速的Alamofire完成处理程序
发布评论