Reputation: 1480
If you wanna test Postman.. You can test on Postman. I couldn't decode data. How can I decode ?
Error:
keyNotFound(CodingKeys(stringValue: "data", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "data", intValue: nil) ("data").", underlyingError: nil))
Model:
// MARK: - CountryResponse
struct CountryResponse: Codable {
let countryData: [CountryData]
enum CodingKeys: String, CodingKey {
case countryData = "data"
}
}
// MARK: - CountryData
struct CountryData: Codable {
let code: String
let currencyCodes: [String]
let name, wikiDataID: String
enum CodingKeys: String, CodingKey {
case code, currencyCodes, name
case wikiDataID = "wikiDataId"
}
}
Service:
class CountryService {
func getAllCountry() {
if let url = URL(string: "https://wft-geo-db.p.rapidapi.com/v1/geo/countries?limit=10") {
var request = URLRequest(url: url)
request.addValue("wft-geo-db.p.rapidapi.com", forHTTPHeaderField: "x-rapidapi-host")
request.addValue("api key", forHTTPHeaderField: "x-rapidapi-key")
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(CountryResponse.self, from: data)
print("response: \(response)")
} catch let error {
print("data decode edilemedi. \(error)")
}
}
task.resume()
} else {
print("hatalı url.")
}
}
}
Upvotes: 1
Views: 3948
Reputation: 2093
I received this error. Here is actual error from xcode console.
valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath:
[CodingKeys(stringValue: "result", intValue: nil), _JSONKey(stringValue:
"Index 152", intValue: 152), CodingKeys(stringValue: "Field",
intValue: nil)], debugDescription: "Expected String value but found null
instead.", underlyingError: nil))
The error tells you which element of the array is missing the data and causing the error. In array "Index 152", the value of Field was null, it was a data entry issue. Maybe some of you will have a similar issue as well.
Swift will complain about null values like this when parsing results using a struct.
Upvotes: 1
Reputation: 13970
This is not the answer to your question, but this is too big for comment, while I think it's important to explain your failure.
It's best to start response handling not from parsing JSON data
, but from
error
is nil
. If error is not nil
, there's no point to continue with parsingresponse
to make sure response.statusCode
is 2xx series (most commonly 200). If it's anything else (e.g. 4xx, 5xx), then the data
will probably contain the error received from the server (or nothing at all), but will definitely not contain JSON you expect.Apple has a good example here:
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
// handle error
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
// handle the error returned by a server
return
}
// now you are ready to look at the data
guard let data = data else { return }
// ...
I think your code will exit either in error
or httpResponse
condition, and that will explain to you what is failing. Also this is a better practice in general.
Upvotes: 1