ios - 代码优化 Swift iOS -(完成处理程序,单例)
问题描述
这是我的 netWorkOperations 课程
import UIKit
class NetworkOpertions: NSObject {
private var actors = [Actor]()
func getMethod(OnCompletion:@escaping (Any)-> Void) {
guard let url = URL(string: "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors")else {return}
let session = URLSession.shared.dataTask(with:url){
(data,response,error) in
if let data = data {
print("This is Data:", data)
do{
let decoder = JSONDecoder()
let downloadedActors = try decoder.decode(Actors.self, from: data)
let res = data
}
OnCompletion(res)
}
catch let err{
print(err.localizedDescription)
// OnCompletion()
}
}
}
session.resume()
}
}
这是我的 ViewController 类
import UIKit
class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate,UIPopoverPresentationControllerDel egate{
private var actors = [Actor]()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Welcome"
tableView.delegate = self
tableView.dataSource = self
downloadJson()
tableView.tableFooterView = UIView()
}
func downloadJson() {
let netWork = NetworkOpertions()
let reponseValue = netWork.getMethod(){ (fetchValue)-> Void in
这里是它的抛出错误:从类型“(_)抛出-> Void”的抛出函数到非抛出函数类型“(Any)-> Void”的无效转换
if fetchValue != nil {
print("MY VAlue:",fetchValue)
let decoder = JSONDecoder()
let downloadedActors = try decoder.decode(Actors.self, from: data)
self.actors = downloadedActors.actors
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return actors.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ActorCell") as? ActorCell else { return UITableViewCell() }
cell.nameLbl.text = actors[indexPath.row].name
cell.DOBLbl.text = actors[indexPath.row].dob
cell.countryCell.text = actors[indexPath.row].country
if let imageURL = URL(string: actors[indexPath.row].image) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.imgView.image = image
}
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
}
请帮助我如何解决此错误:
从 '(_) throws -> Void' 类型的抛出函数到非抛出函数类型 '(Any) -> Void' 的无效转换
解决方案
错误的原因是缺少do catch
换decode
行的块
do {
let downloadedActors = try decoder.decode(Actors.self, from: data)
self.actors = downloadedActors.actors
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch { print(error) }
推荐阅读
- reactjs - 显示警报,然后在 reactjs 中路由
- python - Append Row to Google Spreadsheet-->TypeError: keys must be a string
- python - django.core.exceptions.ImproperlyConfigured:加载 psycopg2 模块时出错:DLL 加载失败:找不到指定的模块
- android - React-Native Promise
- java - 如何使用 Camel 在 AWS sqs 上发送消息属性和消息正文?
- spring-boot - 科特林测试;如何使用抽象测试容器类
- elasticsearch - 如何在grafana中根据用户显示elasticsearch的术语?
- react-native - 反应原生。滚动视图高度
- amazon-web-services - Kubernetes 工作节点上的服务之间的连接失败
- html - Flash 脚本在 Chrome 上的 html 中不起作用