Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代码优化Swift iOS-(完成处理程序,单例) 这是我的网络操作课 这是我的ViewController类_Ios_Swift4 - Fatal编程技术网

代码优化Swift iOS-(完成处理程序,单例) 这是我的网络操作课 这是我的ViewController类

代码优化Swift iOS-(完成处理程序,单例) 这是我的网络操作课 这是我的ViewController类,ios,swift4,Ios,Swift4,这里是它的抛出错误:从类型为“(\ux)throws->Void”的抛出函数到非抛出函数类型“(Any)->Void”的转换无效。 if fetchValue != nil { print("MY VAlue:",fetchValue) let decoder = JSONDecoder() let downloadedActors = try decoder.decode(Actors.self,

这里是它的抛出错误:从类型为“(\ux)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”的抛出函数进行的转换无效 到非抛出函数类型“(任意)->Void”


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

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

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

还有一件事,我在这里使用-任何类型,它是错误的??func getMethod(OnCompletion:@escaping(Any)->Void){因为返回值显然是
Actors
使用它。声明闭包
(Actors?)->Void
并在失败时返回
nil
。其抛出错误:无法将类型“Data”的值转换为预期的参数类型“Actors”?您解码了
getMethod
中已经存在的数据。为什么要在
downloadJson
中再次解码?实际上您可以将
getMethod
中解码的数组分配给e> self.actors并通过
true
false
,判断关闭的成功或失败。
          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
}



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

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