Hello,
I am still new to Firestore and calling api’s. My response keeps coming up nil. what is the disconnect? I’m using Xcode’s playground. Here is the Swift code:
import SwiftUI
struct DeliveryOffer: Codable {
let offers: [Offer]
private enum CodingKeys: String, CodingKey {
case offers = "documents"
}
}
struct Stringvalue: Codable {
let value: String
private enum CodingKeys: String, CodingKey {
case value = "Stringvalue"
}
}
struct Doublevalue: Codable {
let value: Double
private enum CodingKeys: String, CodingKey {
case value = "Doublevalue"
}
}
struct Offer: Codable {
let country: String
let instructions: String
let longitude: Double
let street: String
let brandName: String
let city: String
let latitude: Double
let state: String
let unit: String
let postalCode: String
private enum OfferKeys: String, CodingKey {
case fields
}
private enum FieldsKeys: String, CodingKey {
case country
case instructions
case longitude
case street
case brandName
case city
case latitude
case state
case unit
case postalCode
}
// Create a decoder
// 1. Get the container
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: OfferKeys.self)
let fieldContainer = try container.nestedContainer(keyedBy: FieldsKeys.self, forKey: .fields)
country = try fieldContainer.decode(Stringvalue.self, forKey: .country).value
instructions = try fieldContainer.decode(Stringvalue.self, forKey: .instructions).value
longitude = try fieldContainer.decode(Doublevalue.self, forKey: .longitude).value
street = try fieldContainer.decode(Stringvalue.self, forKey: .street).value
brandName = try fieldContainer.decode(Stringvalue.self, forKey: .brandName).value
city = try fieldContainer.decode(Stringvalue.self, forKey: .city).value
latitude = try fieldContainer.decode(Doublevalue.self, forKey: .latitude).value
state = try fieldContainer.decode(Stringvalue.self, forKey: .state).value
unit = try fieldContainer.decode(Stringvalue.self, forKey: .unit).value
postalCode = try fieldContainer.decode(Stringvalue.self, forKey: .postalCode).value
}
}
let url = "https://firestore.googleapis.com/v1/projects/goe-dash-llc/databases/(default)/documents/users/trCln2nBPi1oMLBjMJ2Z/pickup"
URLSession.shared.dataTask(with: URL(string: url)!) { (data, _, error) in
guard let data = data, error == nil else {
fatalError("No data found")
}
let response = try? JSONDecoder().decode(DeliveryOffer.self, from: data)
if let response = response {
print(response)
}
}.resume()
Here is the JSON tree:
{
"name": "projects/goe-dash-llc/databases/(default)/documents/users/trCln2nBPi1oMLBjMJ2Z/pickup/8ZVFlTyNHa2lCS1Oyro2",
"fields": {
"unit": {
"stringValue": "3050"
},
"street": {
"stringValue": "St. Daphne Dr."
},
"brandName": {
"stringValue": "brandName"
},
"latitude": {
"nullValue": null
},
"postalCode": {
"stringValue": "63301"
},
"instructions": {
"stringValue": "Hello! I am a text field!"
},
"country": {
"stringValue": "USA"
},
"longitude": {
"nullValue": null
},
"city": {
"stringValue": "St. Charles"
},
"state": {
"stringValue": "MO"
}
},
"createTime": "2022-10-03T17:11:00.845679Z",
"updateTime": "2022-10-06T02:27:39.913957Z"
}