Hi, I’m trying to run the build on page 409 and keep getting the following error:
Error fetching interesting photos: keyNotFound(CodingKeys(stringValue: “photo”, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: “No value associated with key CodingKeys(stringValue: “photo”, intValue: nil) (“photo”).”, underlyingError: nil))
Does anyone have any ideas?
import Foundation
class Photo: Codable {
let title: String
let remoteURL: URL
let photoID: String
let dateTaken: Date
enum CodingKeys: String, CodingKey {
case title
case remoteURL = "url_z"
case photoID = "id"
case dateTaken = "datetaken"
}
}
enum EndPoint: String {
case interestingPhotos = "flickr.interestingness.getList"
}
struct FlickrAPI {
private static let baseURLString = "https://api.flickr.com/services/rest"
private static let apiKey = "a6d819499131071f158fd740860a5a88"
private static func flickrURL(endPoint: EndPoint,
parameters: [String:String]?) -> URL {
var components = URLComponents(string: baseURLString)!
var queryItems = [URLQueryItem]()
let baseParams = [
"method": endPoint.rawValue,
"format": "json",
"nojsoncallback": "1",
"api_key": apiKey
]
for (key, value) in baseParams {
let item = URLQueryItem(name: key, value: value)
queryItems.append(item)
}
if let additionalParams = parameters {
for (key, value) in additionalParams {
let item = URLQueryItem(name: key, value: value)
queryItems.append(item)
}
}
components.queryItems = queryItems
return components.url!
}
static var interestingPhotosURL: URL {
return flickrURL(endPoint: .interestingPhotos,
parameters: ["extras": "url_z,date_taken"])
}
import Foundation
enum EndPoint: String {
case interestingPhotos = "flickr.interestingness.getList"
}
struct FlickrAPI {
private static let baseURLString = "https://api.flickr.com/services/rest"
private static let apiKey = "a6d819499131071f158fd740860a5a88"
private static func flickrURL(endPoint: EndPoint,
parameters: [String:String]?) -> URL {
var components = URLComponents(string: baseURLString)!
var queryItems = [URLQueryItem]()
let baseParams = [
"method": endPoint.rawValue,
"format": "json",
"nojsoncallback": "1",
"api_key": apiKey
]
for (key, value) in baseParams {
let item = URLQueryItem(name: key, value: value)
queryItems.append(item)
}
if let additionalParams = parameters {
for (key, value) in additionalParams {
let item = URLQueryItem(name: key, value: value)
queryItems.append(item)
}
}
components.queryItems = queryItems
return components.url!
}
static var interestingPhotosURL: URL {
return flickrURL(endPoint: .interestingPhotos,
parameters: ["extras": "url_z,date_taken"])
}
struct FlickrResponse: Codable {
let photosInfo: FlickrPhotosResponse
enum CodingKeys: String, CodingKey {
case photosInfo = "photo"
}
}
struct FlickrPhotosResponse: Codable {
let photos: [Photo]
enum CodingKeys: String, CodingKey {
case photos = "photo"
}
}
static func photos(fromJSON data: Data) -> Result<[Photo], Error> {
do {
let decoder = JSONDecoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let flickrResponse = try decoder.decode(FlickrResponse.self, from: data)
return .success(flickrResponse.photosInfo.photos)
} catch {
return .failure(error)
}
}
}