代码优化Swift iOS-(完成处理程序,单例)

时间:2018-07-05 11:35:05

标签: ios swift4

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



}

请帮助我如何解决此错误:

  

类型为((_)throws-> Void'的throwing函数的无效转换   到非抛出函数类型'(Any)-> Void'

1 个答案:

答案 0 :(得分:1)

错误的原因是缺少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) }