Hey everyone! I’m pretty new to Swift development and running into a frustrating issue while trying to pull data from Airtable’s API in my SwiftUI app.
I’ve got a basic table with movie names and images. Here’s what my API response looks like:
{
"data":[
{
"recordId":"xyz123ABC",
"timestamp":"2022-11-15T10:45:20.000Z",
"attributes":{
"name":"The Dark Knight",
"poster":"darkKnightPoster"
}
},
{
"recordId":"def456GHI",
"timestamp":"2022-11-15T10:45:21.000Z",
"attributes":{
"name":"Inception",
"poster":"inceptionPoster"
}
}
]
}
I created this structure to decode the response:
struct APIResponse: Decodable {
let data: [DataRecord]
struct DataRecord: Decodable {
let recordId: String
let timestamp: String
let attributes: MovieAttributes
}
struct MovieAttributes: Decodable {
let name: String
let poster: String
}
}
And here’s my basic Movie model:
struct Movie: Identifiable {
let id = UUID()
let name: String
let poster: String
}
My network call looks like this:
class MovieViewModel: ObservableObject {
@Published var movies = [Movie]()
func loadMovies() async {
guard let endpoint = URL(string: "https://api.airtable.com/v0/appXYZ123/Movies") else {
return
}
var apiRequest = URLRequest(url: endpoint)
apiRequest.httpMethod = "GET"
apiRequest.setValue(
"Bearer my_api_token",
forHTTPHeaderField: "Authorization"
)
let networkTask = URLSession.shared.dataTask(with: apiRequest) { responseData, httpResponse, requestError in
if let requestError = requestError {
print(requestError)
} else if
let responseData = responseData,
let httpResponse = httpResponse as? HTTPURLResponse,
httpResponse.statusCode == 200 {
do {
let decodedResponse: APIResponse = try JSONDecoder().decode(APIResponse.self, from: responseData)
DispatchQueue.main.async {
self.movies = []
}
for dataRecord in decodedResponse.data {
DispatchQueue.main.async {
self.movies.append(
Movie(name: dataRecord.attributes.name, poster: String(dataRecord.attributes.poster))
)
}
}
} catch {
print(error)
}
}
}
networkTask.resume()
}
}
But I keep getting this decoding error:
keyNotFound(CodingKeys(stringValue: "name", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "attributes", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"name\", intValue: nil) (\"name\").", underlyingError: nil))
I’ve been stuck on this for hours and tried different approaches but can’t figure out what’s wrong. Anyone have ideas what might be causing this?