SwiftUI Data Retrieval from Airtable Failing to Work as Expected

I’m a beginner with Swift and I’m struggling to retrieve data from Airtable through an API call. My Airtable setup includes a simple table with title and image fields. I set up a ResponseDto struct to decode the JSON response, created a Film struct to use with Identifiable, and configured a UsersViewModel with a fetchUsers() function. However, I keep encountering this error:

keyNotFound(CodingKeys(stringValue: "title", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "records", intValue: nil), _JSONKey(stringValue: "Index 2", intValue: 2), CodingKeys(stringValue: "fields", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\").", underlyingError: nil))

I have spent several hours troubleshooting and researching online without success. Can anyone offer guidance on what might be causing this issue? Appreciate your help!

hey there, i’ve dealt with similar airtable headaches. make sure ur model matches the json exactly. print the raw response to see what’s actually coming back. maybe the ‘title’ field is named differently or nested somewhere unexpected. also, check if all records have a ‘title’ - could be one’s missing it. good luck!

From my experience working with Airtable and SwiftUI, this error often occurs due to a mismatch between your Swift model and the JSON structure from Airtable. Double-check that your ‘title’ field in Airtable exactly matches the property name in your Swift struct. Airtable’s API response typically nests data under ‘fields’, so ensure your decoding accounts for this structure. Consider using a tool like Postman to examine the raw JSON response and verify the exact field names and structure. If the issue persists, you might need to implement a custom decoding strategy or use CodingKeys to map the JSON keys correctly. Logging the raw JSON response in your app can also provide valuable insights for debugging.

Having worked with Airtable and SwiftUI, I encountered a similar situation. The error indicates that your Swift model does not align perfectly with the JSON structure returned by Airtable. It is important to ensure that the field names in your model match exactly with those in Airtable, as they are case-sensitive. Inspecting the raw JSON response using a viewer can reveal deviations in nesting or key naming. Using CodingKeys in your Swift struct to map JSON keys to your property names and applying custom decoding when necessary can help resolve these issues. Debugging with print statements or breakpoints may also pinpoint the exact source of the problem.