首页 > 解决方案 > 代码优化 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' 的无效转换

标签: iosswift4

解决方案


错误的原因是缺少do catchdecode行的块

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

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

推荐阅读