I am trying to implement the Countries API into my app, but I am testing this first to get it correct in playground but keep getting the error:
valueNotFound(__lldb_expr_109.Country, Swift.DecodingError.Context(codingPath: CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), _JSONKey(stringValue: "Index 0", intValue: 0), debugDescription: "Expected Country but found null instead.", underlyingError: nil))
The code for this is: import UIKit import PlaygroundSupport import SwiftUI import Foundation
// MARK: Country
struct Country: Hashable, Codable { var objectId: String var code: String var name: String var native: String var phone: String var capital: String var currency: String var emojiU: String var createdAt: String var updatedAt: String var geonameid: Int var languages: Languages var cities: CountryCities var timezones: Timezones var provinces: Proviences
}
struct Responce: Hashable, Codable { var results: Country }
// MARK: Languages
struct Languages: Hashable, Codable { var __type: String var className: String }
// MARK: CountryCities
struct CountryCities: Hashable, Codable { var __type: String var className: String }
// MARK: Timezones
struct Timezones: Hashable, Codable { var __type: String var className: String }
// MARK: Proviences
struct Proviences: Hashable, Codable { var __type: String var className: String }
var id = "TRTRnWFQ48yxQFImnzZB1fZ0Np9Ex8g4GlFlcQWG" var key = "hGgWjAMWkdJYu2lavC9yTitWavETce0qwkXXxwkM"
func countryAPIRequest() { let filter = """ { "name": { "$exists": true } } """.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) let url = URL(string: "https://parseapi.back4app.com/classes/Continentscountriescities_Country?limit=10&excludeKeys=emoji,continent,shape&where=(filter!)")! var urlRequest = URLRequest(url: url) urlRequest.setValue(id, forHTTPHeaderField: "X-Parse-Application-Id") // This is your app's application id urlRequest.setValue(key, forHTTPHeaderField: "X-Parse-REST-API-Key") // This is your app's REST API key URLSession.shared.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
if let data = data {
do {
let res = try JSONDecoder().decode(Responce.self, from: data)
print(res)
} catch let error {
print(data)
print(error)
}
}
}).resume()
}
countryAPIRequest()
Hey @andysappdesign! Thank you for joining our Database Hub and bringing this up. May I ask why are you retrieving data like that and not using the Parse SDK? It makes things soooo much easier!
Hello! Thank you for the response! Truth be told, this is my first proper project and still got ALOT to learn! I will give Parse SDK a go! Thank you