如何从 Alamofire 返回值

编程入门 行业动态 更新时间:2024-10-13 16:21:06
本文介绍了如何从 Alamofire 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在通过我使用 swift 创建的 API 进行 url 调用,如下所示:

I am making url calls thru an API that I created using swift as follows:

class API { let apiEndPoint = "endpoint" let apiUrl:String! let consumerKey:String! let consumerSecret:String! var returnData = [:] init(){ self.apiUrl = "myurl/" self.consumerKey = "my consumer key" self.consumerSecret = "my consumer secret" } func getOrders() -> NSDictionary{ return makeCall("orders") } func makeCall(section:String) -> NSDictionary{ let params = ["consumer_key":"key", "consumer_secret":"secret"] Alamofire.request(.GET, "(self.apiUrl)/(self.apiEndPoint + section)", parameters: params) .authenticate(user: self.consumerKey, password: self.consumerSecret) .responseJSON { (request, response, data, error) -> Void in println("error (request)") self.returnData = data! as NSDictionary } return self.returnData } }

我在我的 UITableViewController 中调用此 API 以使用 SwiftyJSON 库填充表.但是我的 returnData 从 API 总是空的.Alomofire 调用没有问题,因为我可以成功检索值.我的问题是我应该如何将这个 data 传送到我的表视图控制器?

I call this API in my UITableViewController to populate the table with SwiftyJSON library. However my returnData from the API is always empty. There is no problem with Alomofire calls as I can successfully retrieve value. My problem is how I am supposed to carry this data over to my table view controller?

var api = API() api.getOrders() println(api.returnData) // returnData is empty

推荐答案

正如 Mattt 所指出的,Alamofire 通过完成处理程序"模式异步返回数据,因此您必须这样做.您不能立即返回值,而是希望将方法更改为不返回任何内容,而是使用完成处理程序关闭模式.

As mattt points out, Alamofire is returning data asynchronously via a "completion handler" pattern, so you must do the same. You cannot just return the value immediately, but you instead want to change your method to not return anything, but instead use a completion handler closure pattern.

现在,这可能看起来像:

Nowadays, that might look like:

func getOrders(completionHandler: @escaping (Result<[String: Any]>) -> Void) { performRequest("orders", completion: completionHandler) } func performRequest(_ section: String, completion: @escaping (Result<[String: Any]>) -> Void) { let url = baseURL.appendingPathComponent(section) let params = ["consumer_key": "key", "consumer_secret": "secret"] Alamofire.request(url, parameters: params) .authenticate(user: consumerKey, password: consumerSecret) .responseJSON { response in switch response.result { case .success(let value as [String: Any]): completion(.success(value)) case .failure(let error): completion(.failure(error)) default: fatalError("received non-dictionary JSON response") } } }

然后,当你想调用它时,你使用这个 completion 闭包参数(在尾随闭包中,如果你想的话):

Then, when you want to call it, you use this completion closure parameter (in trailing closure, if you want):

api.getOrders { result in switch result { case .failure(let error): print(error) case .success(let value): // use `value` here } } // but don't try to use the `error` or `value`, as the above closure // has not yet been called //

更多推荐

如何从 Alamofire 返回值

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

发布评论

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

>www.elefans.com

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