代码优化Swift iOS-(完成处理程序,单例) 这是我的网络操作课 这是我的ViewController类
这里是它的抛出错误:从类型为“(\ux)throws->Void”的抛出函数到非抛出函数类型“(Any)->Void”的转换无效。代码优化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,
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
中解码的数组分配给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) }