I'm starting to hook up my frontend SwiftUI project to a backend and so I'm learning how to make API calls in Swift. I began by trying to use the code in this video as a workable example. However, I'm running into an error where the JSONDecoder is failing to decode the returned data.
What is going on here? My apologies for the blunt question--I'm new to backend development.
let url = "https://api.sunrise-sunset.org/json?date=2020-01-01&lat=-74.060&lng=40.7128&form"
getData(from: url)
private func getData(from url: String) {
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
guard let data = data, error == nil else {
print("Something Went Wrong...")
return
}
// data is available
var result: Response?
do {
result = try JSONDecoder().decode(Response.self, from: data)
} catch {
print("failed to convert \(error.localizedDescription)")
}
guard let json = result else {
return
}
print(json.status)
print(json.results.sunrise)
})
task.resume()
}
struct Response: Codable {
let results: MyResult
let status: String
}
struct MyResult: Codable {
let sunrise: String
let sunset: String
let solar_noon: String
let day_length: Int
let civil_twilight_begin: String
let nautical_twilight_begin: String
let nautical_twilight_end: String
let astronomical_twilight_begin: String
let astronomical_twilight_end: String
}
Note that I'm not following the video exactly. I took the getData function out of a
ViewController class because I didn't think it was relevant--I'm not dealing with any views at this point, and I don't want to be either. I also took the liberty to testing the code in a Swift Playground. I don't know whether that will influence things.