使用单独的类在Swift中为TableView处理Alamofire响应的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-18 05:53:56
本文介绍了使用单独的类在Swift中为TableView处理Alamofire响应的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个类来处理我的服务器请求,这些请求返回JSON以填充TableView。这是我第一次做这种事情,我很好奇在这里使用的最佳范例是什么。像委派这样的事情比使用dispatch_async更好吗? Alamofire的响应几乎是异步的,因此我无法从中返回数据。由于我的请求是在共享环境中发生的(它存在于我创建的框架中,所以我可以在多个目标中使用它)ServerManager类,因此我需要以某种方式将其发送到TableView,但我不确定哪种最佳方式去做。

I'm writing a class to handle my server requests that return JSON to populate a TableView. This is my first time doing this sort of thing, and I was curious what is the be best paradigm to use here. Is something like delegation better than using dispatch_async? Pretty much Alamofire's response is asynchronous, so I can't return data out of it. Since my request is happening in a shared(it exists in a framework I created so I could use it in multiple targets) ServerManager class, I need to get it to the TableView some how, and I'm not sure what the best way to do that.

委派后台线程的优点是什么,反之亦然?我知道这个问题可能在这里被问到很多,但是我在搜索时似乎找不到很好的解释。

What are the pros of delegation over background threading and vice versa? I know this question probably gets asked a lot around here, but I couldn't seem to find a good explanation when I was searching.

推荐答案

ServerManager 中的方法应传递给闭包(块)。

The method in ServerManager should be passed a closure (block). This requires no delegation and no dispatches in the view controller.

class ServerManager { func fetchObjectsWithOptions(options: [AnyObject], completion: (items: [AnyObject], error: ErrorType?) -> Void) { // Use the options to setup and make the request // Be sure it executes on the main thread completion(items: items, error: nil) // Any finishing needed } } // ... class MyTableViewController: UITableViewController { lazy var serverManager = ServerManager() var items: [AnyObject] = [] override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) serverManager.fetchObjectsWithOptions([]) { items, error in if error == nil { self.items = items self.tableView.reloadData() } } } }

闭包是可以分配给变量的函数。在Swift中,闭包相对简单。

Closures are functions which can be assigned to a variables. Closures are relatively simple in Swift. Here is a closure that takes no parameters and has a void return type.

{ () -> Void in print("foo") }

下面,变量 x 的类型签名为()->无效,并被分配了闭包。仅通过调用函数 x()即可执行关闭操作。

Below, the variable x has the type signature of () -> Void, and is assigned the closure. Executing the closure is done just line calling a function, x().

let x: () -> Void = { () -> Void in print("foo") } x() // prints foo

作为函数参数传递。调用 funcWithClosure()时,它将执行闭包。

Closures can be passed around as function parameters. When funcWithClosure() is called, it executes the closure.

func funcWithClosure(x: () -> Void) { x() } funcWithClosure({ () -> Void in print("foo") })

采用参数的闭包将参数及其类型指定为闭包类型的一部分。

Closures which take parameters have the parameters and their types specified as part of the closure type.

func funcWithClosure2(x: (string: String) -> Void) { x(string: "foo") // <-- parameters must be named } funcWithClosure2({ (string: String) -> Void in print(string) })

类型推断引擎允许您从闭包中删除类型。

The type inference engine allows you to remove the type from the closure.

funcWithClosure({ print("foo") }) // <-- No type declaration funcWithClosure2({ string in print(string) }) // <-- Only parameter name

此外,如果闭包是最后一个参数,则不需要在闭包周围加上括号

In addition, if a closure is the last parameter, you don't need the parentheses around the closure.

funcWithClosure { print("foo") }

最后,这是一个以闭包结尾的多个参数的示例。

Finally, here is an example with multiple parameter ending with a closure.

func funcWithString(string: String, closure: (string: String) -> Void) { closure(string: string) } funcWithString("foo", closure: { (string: String) -> Void in print(string) })

或者,您可以使用不太冗长的语法。

Or, you can use the less verbose syntax.

funcWithString("foo") { string in print(string) }

更多推荐

使用单独的类在Swift中为TableView处理Alamofire响应的最佳方法是什么?

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

发布评论

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

>www.elefans.com

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