这是我的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'
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
}
}请帮助我解决这个错误:
从“(_)抛出->空”类型的抛出函数到非抛出函数类型“(任意) ->空”的无效转换
发布于 2018-07-05 11:44:32
造成此错误的原因是缺少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) } https://stackoverflow.com/questions/51190333
复制相似问题