Different issue: not getting any failures! What I mean is that this line in the description says that we should get some unloadable URLs:
“Some images in the data source are intentionally mis-named, so that there are instances where an image fails to download to exercise the failure case.”
I took a look at the downloaded list of images, and the 4 entries at the end have “NO URL” as the url.
At this point in fetchPhotoDetails, those are effectively filtered out:
// 4
for (name, value) in datasourceDictionary {
let url = URL(string: value)
if let url = url {
let photoRecord = PhotoRecord(name: name, url: url)
self.photos.append(photoRecord)
}
}
URL(string: value) is returning nil for them, because “NO URL” is not an allowable URL, due to the blank space. It can be fixed like this, making the blank space into an underscore:
for (name, value) in datasourceDictionary {
let fixedUrl = value.replacingOccurrences(of: " ", with: "_")
let url = URL(string: fixedUrl)
if let url = url {
let photoRecord = PhotoRecord(name: name, url: url)
self.photos.append(photoRecord)
}
}
But that’s not all. You also have this in func main() of ImageDownloader:
//5
guard let imageData = try? Data(contentsOf: photoRecord.url) else { return }
Because it returns when the url is no good, it never gets a chance to set the state of photoRecord.
So you need this instead:
//5
let imageData = try? Data(contentsOf: photoRecord.url)
and a little further down, change the last bit to this:
//7
if let imageData = imageData {
photoRecord.image = UIImage(data:imageData)
photoRecord.state = .downloaded
} else {
photoRecord.state = .failed
photoRecord.image = UIImage(named: "Failed")
}
Now you can have some failures downloading! You will get a nice red warning image for the likes of “Flying Taxi” and “Chocolate House”. I tweaked the cellForRow switch on state to at least show the failing name:
case .failed:
indicator.stopAnimating()
cell.textLabel?.text = "Failed to load \(photoDetails.name)"