Cannot get number of photos


#1

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


#2

Paul,

I’m not seeing it just by looking at this code here, but if you .zip up your code for the project I’d be happy to help debug it and get to the root of the issue.


#3

Hi Mikey

Thanks for the offer to look over my code. You’ll find it at http://web-cars.com/BNR/Photorama.zip

I’m looking at FlickrAPI.swift, specifically at // Maybe the JSON format for photos has changed
Has it changed? How would I know?

I can’t figure out why finalPhotos is empty.

The line:
if let photo = photo(fromJSON: photoJSON) {
does not get executed, from what I can see and photosArray is full of content. Beyond that, I’m stumped.

You’ll see a lot of print() statements commented out, that’s how I’ve been approaching the problem.

~paul


#4

I think I see the problem. This is a common mistake when using the Flickr API, unfortunately. They’re inconsistent with the “date_taken” and “datetaken” keys in their JSON.

On line 34 of FlickrAPI.swift, you refer tojson["dateTaken"], but it should be json["datetaken"] (lowercase ‘t’ in ‘taken’. This is the eternal problem with string-based problems like JSON parsing, since the compiler can’t tell you when something like this doesn’t match what the server is sending.


#5

To further explain, we have a huge guard statement here, and are bailing out of the processing of any given photo if it doesn’t have the “datetaken” key in its dictionary. Because we were asking for the “dateTaken” key instead, which none of the photos have, the guard statement bailed for literally every photo.


#6

Yep, that was it. I now have “Successfuly found 91 photos.”. The book has it correctly as json[“datetaken”] so I’ve got nobody to blame but myself.

Thank you muchly,

~paul