diff --git a/QuizApp/Topic.swift b/QuizApp/Topic.swift index b8fe44b..3cd3c95 100644 --- a/QuizApp/Topic.swift +++ b/QuizApp/Topic.swift @@ -13,16 +13,39 @@ struct QuizQuestion: Decodable, Hashable, Identifiable { let answers: [String] let correctAnswer: Int + let type: String? + let minValue: Double? + let maxValue: Double? + let correctValue: Double? + let unit: String? + // Manuelles Decoding, id wird generiert init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) + question = try container.decode(String.self, forKey: .question) - answers = try container.decode([String].self, forKey: .answers) - correctAnswer = try container.decode(Int.self, forKey: .correctAnswer) + + // Falls im JSON nicht vorhanden, Standard setzen + answers = (try? container.decode([String].self, forKey: .answers)) ?? [] + correctAnswer = (try? container.decode(Int.self, forKey: .correctAnswer)) ?? 0 + + // Optional + type = try container.decodeIfPresent(String.self, forKey: .type) + minValue = try container.decodeIfPresent(Double.self, forKey: .minValue) + maxValue = try container.decodeIfPresent(Double.self, forKey: .maxValue) + correctValue = try container.decodeIfPresent(Double.self, forKey: .correctValue) + unit = try container.decodeIfPresent(String.self, forKey: .unit) + id = UUID() } private enum CodingKeys: String, CodingKey { case question, answers, correctAnswer + case type, minValue, maxValue, correctValue, unit + } + + var isEstimation: Bool { + if let t = type?.lowercased() { return t == "estimation" } + return !answers.isEmpty ? false : (minValue != nil || maxValue != nil || correctValue != nil) } } diff --git a/QuizApp/data.json b/QuizApp/data.json index a1ab7ac..fc6f5a6 100644 --- a/QuizApp/data.json +++ b/QuizApp/data.json @@ -182,5 +182,23 @@ ], "correctAnswer": 1 } + ], + "Schätzen": [ + { + "type": "estimation", + "question": "Wie hoch ist der Eiffelturm?", + "minValue": 0, + "maxValue": 1000, + "correctValue": 330, + "unit": "m" + }, + { + "type": "estimation", + "question": "In welchem Jahr begann der Bau des Kölner Doms?", + "minValue": 1000, + "maxValue": 2025, + "correctValue": 1248, + "unit": "Jahr" + } ] }