Last night I was able to use the fetchInterestingPhotos Method to return a network call and could see all the json in the bottom terminal as well as an image on the imageView. Today however now that the imageView was removed completely (I have no lingering outlets) and I added the CollectionView my store.fetchInterestingPhotos now gives an error
Photorama/PhotosViewController.swift:24: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2021-12-24 01:01:20.187418-0500 Photorama[4291:199801] Photorama/PhotosViewController.swift:24: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
(lldb)
import UIKit
class PhotosViewController: UIViewController {
@IBOutlet var collectionView: UICollectionView!
var store: PhotoStore!
let photoDataSource = PhotoDataSource()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = photoDataSource
//This method used to work... below is the line that returns nil
store.fetchInterestingPhotos {
(photosResult) in
switch photosResult {
case let .success(photos):
print("Successfully found \(photos.count) photos")
self.photoDataSource.photos = photos
case let .failure(error):
print("Error fetching interesting photos: \(error)")
self.photoDataSource.photos.removeAll()
}
self.collectionView.reloadSections(IndexSet(integer: 0))
}
}
}
I’m not sure why removing the imageView to add a CollectionView would result in now the store property returning nil when it was just working fine to show me interestingPhotos with a single imageview. Below is the PhotoStore.
import UIKit
enum PhotoError: Error {
case imageCreationError
case missingImageURL
}
class PhotoStore {
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()
}
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)
}
}
Any help with this is much appreciated Would love to get past this little hurdle. Thanks