52 lines
1.7 KiB
Swift
52 lines
1.7 KiB
Swift
//
|
|
// QuizQuestion.swift
|
|
// QuizApp
|
|
//
|
|
// Created by Paul on 03.08.25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct QuizQuestion: Decodable, Hashable, Identifiable {
|
|
let id: UUID
|
|
let question: String
|
|
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)
|
|
|
|
// 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)
|
|
}
|
|
}
|