Fatal Error while Trying to Load Photos into the Interface

Hi! I’m trying to have the photos that my code previously found load into the interface. I added the code on pages 412-414 then ran it. The app had successfully found 100 photos but then crashed and gave me this error:

Successfully found 100 photos

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/sarahchang/Desktop/Summer Projects/Photorama/Photorama/PhotosViewController.swift, line 23

2020-08-04 13:37:46.919909-0700 Photorama[44242:2514906] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file /Users/Danvers25/Desktop/Summer Projects/Photorama/Photorama/PhotosViewController.swift, line 23

(lldb)

Does anybody have any ideas? The line “self.imageView.image = image” is the line it says there is an error on. What am I missing? TIA!

PhotosViewController.Swift:

@IBOutlet private var imageView: UIImageView!
  var store: PhotoStore!

func updateImageView(for photo: Photo) {
    store.fetchImage(for: photo) {
        (imageResult) in
        
        switch imageResult {
        case let .success(image):
            self.imageView.image = image
        case let .failure(error):
            print("Error downloading image: \(error)")
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    store.fetchInterestingPhotos {
        (photosResult) in
        
        switch photosResult {
        case let .success(photos):
            print("Successfully found \(photos.count) photos")
            if let firstPhoto = photos.first {
                self.updateImageView(for: firstPhoto)
            }
        case let .failure(error):
            print("Error fetching interesting photos: \(error)")
        }
    }
    }

PhotoStore.Swift:

 enum PhotoError: Error {
case imageCreationError
case missingImageURL


private let session: URLSession = {
    let config = URLSessionConfiguration.default
    return URLSession(configuration: config)
}()

func fetchInterestingPhotos(completion: @escaping (Result<[Photo], Error>) -> Void) {
    
    let url = FlickrAPI.interestingPhotosURL
    let request = URLRequest(url: url)
    let task = session.dataTask(with: request) {
        (data, response, error) in
        
        let result = self.processPhotosRequest(data: data, error: error)
        OperationQueue.main.addOperation {
        completion(result)
        }
    }
    task.resume()
}

private func processPhotosRequest(data: Data?,
                                  error: Error?) -> Result<[Photo], Error> {
    guard let jsonData = data else {
        return .failure(error!)
    }
    
    return FlickrAPI.photos(fromJSON: jsonData)
}

func fetchImage(for photo: Photo,
                completion: @escaping (Result<UIImage, Error>) -> Void) {
    guard let photoURL = photo.remoteURL else {
        completion(.failure(PhotoError.missingImageURL))
        return
    }
    let request = URLRequest(url: photoURL)
    
    let task = session.dataTask(with: request) {
        (data, response, error) in
        
        let result = self.processImageRequest(data: data, error: error)
        OperationQueue.main.addOperation {
        completion(result)
        }
    }
    task.resume()
}

private func processImageRequest(data: Data?,
                                 error: Error?) -> Result<UIImage, Error> {
    guard
        let imageData = data,
        let image = UIImage(data: imageData) else {
            
            // Couldn't create an image
            if data == nil {
                return .failure(error!)
            } else {
                return .failure(PhotoError .imageCreationError)
            }
    }
    
    return .success(image)
}

Check that your imageView IBOutlet is connected to the UIImageView element in the storyboard.

That worked! Thanks for all of your help! You’re so awesome!