了解快速的Alamofire完成处理程序

编程入门 行业动态 更新时间:2024-10-12 08:23:06
本文介绍了了解快速的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.

  • func authenticateUser具有参数completedler:(responseObject:String ?, error:NSError?)->(),是这是对completionHandler方法的引用吗?还是物体? ->()的目的是什么?
  • 当我调用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完成处理程序

    本文发布于:2023-11-26 02:02:20,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1632205.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:快速   程序   Alamofire

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!