iOS Swift - Networking
You want to load an image from a URL and display it in a UIImageView safely, handling errors and updating UI correctly. Which code snippet correctly achieves this?
let url = URL(string: "https://example.com/image.png")
URLSession.shared.dataTask(with: url!) { data, response, error in
if let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
imageView.image = image
}
} else {
print("Failed to load image")
}
}.resume()