I'm getting back the following error when attempting to call data for VA facility information.
failure(Swift.DecodingError.valueNotFound(
Swift.Double, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "attributes", intValue: nil),
CodingKeys(stringValue: "wait_times", intValue: nil),
CodingKeys(stringValue: "health", intValue: nil), _JSONKey(stringValue: "Index 6", intValue: 6),
CodingKeys(stringValue: "new", intValue: nil)], debugDescription: "Expected Double value but found null instead.", underlyingError: nil)))
I check with Postman and the data is present so I'm not entirely sure why the data is failing.
Here's part of my model
// MARK: - Welcome
struct Welcome: Codable, Identifiable {
let id = UUID()
let data: DataClass
}
// MARK: - DataClass
struct DataClass: Codable {
let id, type: String
let attributes: Attributes
}
// MARK: - Attributes
struct Attributes: Codable {
let name, facilityType, classification: String
let website: String
let lat, long: Double
let timeZone: String
let address: Address
let phone: Phone
let hours: Hours
let operationalHoursSpecialInstructions: String
let services: Services
let satisfaction: Satisfaction
let waitTimes: WaitTimes
let mobile: Bool
let activeStatus: String
let operatingStatus: OperatingStatus
let detailedServices: [DetailedService]
let visn: String
enum CodingKeys: String, CodingKey {
case name
case facilityType = "facility_type"
case classification, website, lat, long
case timeZone = "time_zone"
case address, phone, hours
case operationalHoursSpecialInstructions = "operational_hours_special_instructions"
case services, satisfaction
case waitTimes = "wait_times"
case mobile
case activeStatus = "active_status"
case operatingStatus = "operating_status"
case detailedServices = "detailed_services"
case visn
}
}
Here's my function call
typealias vaFacilityData = [Welcome]
Class .... {
@Published var vaFacilityData: [Welcome] = []
func getVAFacilityInfo(locationURL: String) {
var URLBuilder = URLComponents(string: locationURL)
guard let url = URLBuilder?.url else { return }
var request = URLRequest(url: url)
// request.httpMethod = "GET"
request.setValue(VA_API_Key, forHTTPHeaderField: "apikey")
print("\(#function) url \(locationURL)")
URLSession.shared.dataTaskPublisher(for: request)
.subscribe(on: DispatchQueue.global(qos: .background))
.receive(on: DispatchQueue.main)
.tryMap { (data, response) -> Data in
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 && response.statusCode < 300 else {
self.appError = AppError(errorString: "\(UserFetchError.badServerResponse)")
throw UserFetchError.badServerResponse
}
print("\(#function) response \(response.statusCode)")
print("\(#function) returning data \(data)")
return data
}
.decode(type: Welcome.self, decoder: JSONDecoder())
.sink { (completion) in
print("\(#function) completion - \(completion)")
} receiveValue: { (returnedData) in
print("\(#function) returnedData - \(returnedData)")
self.vaFacilityData = [returnedData]
}
.store(in: &cancellabes)
}
}
}