Still going thru chapter 20, preparing for advanced iOS course starting Sep-18.
in PhotosViewController.swift I have:
store.fetchInterestingPhotos {
(photosResult) -> Void in
switch photosResult {
case let .success(photos):
print("Successfuly found \(photos.count) photos.")
// if let firstPhoto = photos.first {
// self.updateImageView(for: firstPhoto)
// }
case let .failure(error):
print(“Error fetching interesting photos: (error) (PhotosViewController.swift)”) // Appears
}
}
}
When run, I see “Error fetching interesting photos:” in the console, not the number of photos that page 599 has me looking for.
In FlickrAPI.swift I have:
static func photos(fromJSON data: Data) -> PhotosResult {
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
print("jsonObject: \(jsonObject)") // OK
guard
let jsonDictionary = jsonObject as? [AnyHashable: Any],
let photos = jsonDictionary["photos"] as? [String: Any],
let photosArray = photos["photo"] as? [[String: Any]] else {
print("static func photos (else FlickrAPI.swift") // Does not appear
// The JSON structure doesn't match our expectations
return .failure(FlickrError.invalidJSONData as Error)
}
var finalPhotos = [Photo]()
print("photosArray: \(photosArray)") // Lots, nicely organized
for photoJSON in photosArray {
if let photo = photo(fromJSON: photoJSON) {
print("if let photo = photo(fromJSON: photoJSON)") // not there
finalPhotos.append(photo)
}
}
// print(“photosArray: (photosArray)”) // lots, not well organized
print(“finalPhotos: (finalPhotos)”) // 0 values
if finalPhotos.isEmpty && !photosArray.isEmpty { // page 596
print(“if finalPhotos.isEmpty && !photosArray.isEmpty”) // There
// We weren’t able to parse any of the photos
// Maybe the JSON format for photos has changed
return .failure(FlickrError.invalidJSONData)
}
return .success(finalPhotos)
} catch let error {
return .failure(error)
}
}
}
So why do I have a failure? I’m struggling to figure out why I am not executing at if let photo = photo(fromJSON: photoJSON), and I think that is where my failure is - I am not executing finalPhotos.append(photo) - true?
Any ideas? I’m hoping to get as much done as possible B4 class starts!
Thanks!
~paul