在Xcode中展开UIImage(data:data)时发生致命错误

时间:2019-05-29 02:21:48

标签: swift xcode core-data uiimage

我有一个负责显示联系人列表的表视图,但是,每当我尝试加载该表视图时,我的项目便会崩溃,并显示以下错误|致命错误:在展开可选值时意外发现nil | 这就是我目前有关加载数据的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell",
    for: indexPath) as!TableClass
  let contacts = contact[indexPath.row]
  cell.Name.text = contacts.name
  cell.Picture.image = UIImage(data: contacts.image!, scale: 1) //this is the line of code that seems to make my project crash
  return cell
}

我还试图使用诸如此类的case方法:

if case let cell.Picture.image = UIImage(data: contacts.image!, scale: 1) {
  print("Hello")
}

但是,当我尝试执行此操作时,项目仍然崩溃并且Xcode指向if case let行并报告相同的错误

1 个答案:

答案 0 :(得分:0)

您应该使用“可选绑定”来获取可选图像。

if let image = contacts.image {
    cell.Picture.image = UIImage(data: image, scale: 1)
} else {
    //use default placeholder image
}