DEV Community

Liang Wang
Liang Wang

Posted on

UIImage(data: data)

Image(uiImage: UIImage(data: training.image!)!)
        .resizable()
        .scaledToFill()

Enter fullscreen mode Exit fullscreen mode

This code is dangerous as it is force unwrapping training.image, it would crash the app when it is nil.

I need to provide a default image to fall back on safely.

My solution for now is use if let

if let data = training.image {
        let uiImage = UIImage(data: data)!
        Image(uiImage: uiImage)
          .resizable()
          .scaledToFill()
      }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)