Hello, I can't show the questions on the screen. I made Text(triviaManager.question) in View but it doesn't show the text on the screen
JSON
[
{
"description": 0,
"disid": 13,
"question": "Hypertension",
"lastmonth": 0
},
{
"description": 0,
"disid": 14,
"question": "Diabetes",
"lastmonth": 0
},
{
"description": 0,
"disid": 15,
"question": "Rheumatic Disease",
"lastmonth": 0
}
]
struct Trivia: Decodable {
var results: [Result]
struct Result: Decodable, Identifiable {
var id: UUID {
UUID()
}
var description: Int
var disid: Int
var question: String
var lastmonth: Int
var formattedQuestion: AttributedString {
do {
return try AttributedString(markdown: question)
} catch {
print("Error setting formattedQuestion: \(error)")
return ""
}
}
}
}
class TriviaManager
func fetchTrivia(token: String, email: String) async {
guard let url = URL(string: "http://ec2-3-74-42-117.eu-central-1.compute.amazonaws.com:8080/dentistodayapis/getanamnez/80") else { fatalError("Missing URL") }
var urlRequest = URLRequest(url: url)
urlRequest.addValue("\(token)", forHTTPHeaderField: "credentialtoken")
urlRequest.addValue("\(email)", forHTTPHeaderField: "username")
do {
let (data, response) = try await URLSession.shared.data(for: urlRequest)
guard (response as? HTTPURLResponse)?.statusCode == 200 else { fatalError("Error while fetching data") }
let decoder = JSONDecoder()
// Line below allows us to convert the correct_answer key from the API into the correctAnswer in our Trivia model, without running into an error from the JSONDecoder that it couldn't find a matching codingKey
decoder.keyDecodingStrategy = .convertFromSnakeCase
let decodedData = try decoder.decode(Trivia.self, from: data)
DispatchQueue.main.async {
// Reset variables before assigning new values, for when the user plays the game another time
self.index = 0
self.trivia = decodedData.results
// self.length = self.trivia.count
self.setQuestion()
}
} catch {
print("Error fetching : \(error)")
}
}