Errors around Listing 22.1

I believe I found a couple problems in the book around Listing 22.1, which gives this code:

func fetchImage(for photo: Photo,
                completion: @escaping (Result<UIImage, Error>) -> Void) {

    guard let photoKey = photo.photoID else {
        preconditionFailure("Photo expected to have a photoID.")
    }

    let request = URLRequest(url: photoURL)

At this point in the project, the line let request = URLRequest(url: photoURL) does not appear this high up in the function. Instead, that line should read if let image = imageStore.image(forKey: photoKey) {. The solutions files provided for this book confirm this. While this may seem like a minor issue, be careful not to let it confuse you while working through this step of the project.

The second issue is more significant. Right after Listing 22.1 the book says:

Build the project. While you will still have errors in the project, the errors in fetchImage(for:completion:) should be resolved.

I found this to be incorrect. Building the project at this point did not resolve the errors for that function; I still had three errors. One was saying that the Photo type specified in the function’s first argument could not be found in scope. The body of the function had two more errors complaining that Type of expression is ambiguous without more context about the photoID and remoteURL properties.

Screen Shot 2021-07-05 at 12.44.23 AM

I think this is because the Photo type is undefined at this point in the project. (We just refactored the old Photo type and renamed it to FlickrAPI). So it makes sense that the compiler can’t find a Photo type or its properties.

Later in this chapter we will be creating two new files, Photo+CoreDataClass.swift and Photo+CoreDataProperties.swift. In Photo+CoreDataClass the Photo type will be declared. But at this point in the chapter, we have not yet taken that step. So the book is erroneously states that the errors in fetchImage(for:completion:) should be resolved.

I wanted to warn people about this because these issues confused me and I spent a lot of time trying to figure out why my project was still having these errors.