gpt4 book ai didi

ios - 代码优化 Swift iOS - (Completion Handler, singleton)

转载 作者:行者123 更新时间:2023-11-29 11:33:37 25 4
gpt4 key购买 nike

这是我的 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

这是它的抛出错误:从抛出函数类型 '(_) throws -> 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
}



}

请帮我解决这个错误:

Invalid conversion from throwing function of type '(_) throws -> Void' to non-throwing function type '(Any) -> Void'

最佳答案

错误的原因是缺少 do catch block 包装 decode

do {
let downloadedActors = try decoder.decode(Actors.self, from: data)
self.actors = downloadedActors.actors

DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch { print(error) }

关于ios - 代码优化 Swift iOS - (Completion Handler, singleton),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51190333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com