UIImage(data: data)

hejliang

Liang Wang

Posted on February 2, 2024

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
💖 💪 🙅 🚩
hejliang
Liang Wang

Posted on February 2, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related