I am working on a project involving Earthquakes, I have made a call to this URL of Earthquakes with a magnitude of 4.5 and greater and the Data gets decoded correctly. I see the response on the console: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_week.geojson
The problem lies when i use another URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_week.geojson . This URL returns the same properties as the above URL but it doesn't decode correctly.
Things i tried:
- Making the properties of the Response optional
- Creating a seperate Model for Response
This is what i how is my Model:
struct Earthquake: Codable {
let type: String?
let metadata: Metadata?
let features: [Feature]
let bbox: [Double]?
}
// MARK: - Metadata
struct Metadata: Codable {
let generated: Int
let url: String
let title: String
let status: Int
let api: String
let count: Int
}
// MARK: - Feature
struct Feature: Codable {
let type: String
let properties: Properties
let geometry: Geometry
let id: String?
}
// MARK: - Properties
struct Properties: Codable {
let mag: Double?
let place: String?
let time, updated: Int?
let tz: Int?
let url: String?
let detail: String?
let felt: Int?
let cdi, mmi: Double?
let alert: String?
let status: String?
let tsunami, sig: Int?
let net: String?
let code, ids: String?
let sources: String?
let types: String?
let nst: Int?
let dmin: Double?
let rms: Double
let gap: Int?
let magType: String?
let type: String?
let title: String?
}
// MARK: - Geometry
struct Geometry: Codable {
let type: String
let coordinates: [Double]
}
This is how i call it.
let topEarthQuakesURL = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_week.geojson")!
let task = URLSession.shared.dataTask(with: topEarthQuakesURL) { (data, response, error) in
guard let data = data else {return}
print(data)
let decoder = JSONDecoder()
let EarthquakeData = try! decoder.decode(Earthquake.self, from: data)
print(EarthquakeData.features[0].geometry.coordinates)
}
task.resume()
i get this error. Notice that it print's out 1064216 bytes
. Which tells me that i am getting the data, it is just not decoding correctly at one point. Just not sure where.
1064216 bytes
Fatal error: 'try!' expression unexpectedly raised an error:
Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath:
[CodingKeys(stringValue: "features", intValue: nil), _JSONKey(stringValue: "Index 29", intValue:
29), CodingKeys(stringValue: "properties", intValue: nil), CodingKeys(stringValue: "gap",
intValue: nil)], debugDescription: "Parsed JSON number <231.67> does not fit in Int.",
underlyingError: nil)): file
/Users/olerma/Documents/EarthQuakes/Urth/Urth/Controller/MainViewController.swift, line 33