0

I have a tableview that is responsible for showcasing a contacts list, however whenever I attempt to load the tableview my project crashes with the error |Fatal error: Unexpectedly found nil while unwrapping an Optional value| this is what my code regarding loading the data looks like as of right now

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
}

I've also attempted to use a case method such as this:

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

However when I attempt this the project still crashes and Xcode point to the if case let line and reports the same error

Faisal Rahman Avash
  • 1,268
  • 9
  • 13
Tariq Khan
  • 27
  • 2
  • 6

1 Answers1

0

You should use Optional Binding to get the optional image.

if let image = contacts.image {
    cell.Picture.image = UIImage(data: image, scale: 1)
} else {
    //use default placeholder image
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70