How to validate quiz answers using Airtable API in Swift iOS app

I’m building a vocabulary quiz app in Swift and using Airtable as my backend database. I managed to get the questions and choices displaying correctly, but I’m having trouble with answer validation.

Here’s my current code for loading quiz data:

func loadQuizData() {
    let endpoint = "https://api.airtable.com/v0/appXYZ123/QuizTable?maxRecords=15&view=Main"
    let token = "myToken"
    let requestURL = URL(string: endpoint)!
    var apiRequest = URLRequest(url: requestURL)
    apiRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    
    URLSession.shared.dataTask(with: apiRequest) { (responseData, urlResponse, requestError) in
        let jsonDecoder = JSONDecoder()
        guard let responseData = responseData else { return }
        do {
            let quizData = try jsonDecoder.decode(QuizResponse.self, from: responseData)
            DispatchQueue.main.async {
                self.questionCounter.text = "Question \(self.currentQuestion+1)"
                self.questionText.text = quizData.records[self.currentQuestion].fields.questionText
                self.choiceOne.setTitle(quizData.records[self.currentQuestion].fields.choiceOne, for: .normal)
                self.choiceTwo.setTitle(quizData.records[self.currentQuestion].fields.choiceTwo, for: .normal)
                self.choiceThree.setTitle(quizData.records[self.currentQuestion].fields.choiceThree, for: .normal)
                self.choiceFour.setTitle(quizData.records[self.currentQuestion].fields.choiceFour, for: .normal)
            }
        } catch {
            print("Error: \(error)")
        }
    }.resume()
}

The problem happens when I try to check if the user picked the right answer. My quiz data becomes nil and crashes the app. How can I properly store and access the correct answers for comparison?

Your app crashes because you’re trying to access quiz data that’s already been deallocated. You can’t just use it once in the network callback - you need to store it properly.

Add a property to hold your quiz data:

var quizRecords: [QuizRecord] = []

Update your loadQuizData function to actually store the data:

let quizData = try jsonDecoder.decode(QuizResponse.self, from: responseData)
self.quizRecords = quizData.records
DispatchQueue.main.async {
    self.displayQuestion()
}

Make a separate function for displaying questions:

func displayQuestion() {
    guard currentQuestion < quizRecords.count else { return }
    let record = quizRecords[currentQuestion]
    questionCounter.text = "Question \(currentQuestion+1)"
    questionText.text = record.fields.questionText
    choiceOne.setTitle(record.fields.choiceOne, for: .normal)
    // ... other choices
}

For answer validation, compare against your stored data:

func validateAnswer(_ selectedChoice: String) -> Bool {
    return selectedChoice == quizRecords[currentQuestion].fields.correctAnswer
}

This pattern crashes apps all the time. Keep your data alive beyond the network call and you’ll be fine.