了解快速的Alamofire完成处理程序 [英] Understanding swift Alamofire completionHandler

查看:58
本文介绍了了解快速的Alamofire完成处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.


  1. func authenticateUser具有参数completedler:(responseObject:String ?, error:NSError?)->(),是这是对completionHandler方法的引用吗?还是物体? ->()的目的是什么?

  2. 当我调用authenticateUser函数时,我实际上如何访问响应?我的任何一个API函数都没有任何回报,..}语法中的funcname {(parameter,parameter)似乎真的很奇怪。


推荐答案

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完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆