I have some static data for a list of "Badges" that I'm able to parse with JSONDecoder. I want to add a state that is not in the json data and when I added the variable to the Badge struct, parsing failed.
I then tried to create a two different structs, BadgeData which is parsed and Badge which contains state variable and BadgeData. But how do I merge this with state variable in a good way? Is there a better structure for this?
import Foundation
final class ModelData: ObservableObject {
@Published var badges: [Badge] = // ??
var badgeData: [BadgeData] = load("badges.json")
var taken: [Int] = load("taken.json")
}
struct BadgeData: Hashable, Codable, Identifiable {
let id: Int
let title: String
let imageRef: String
let requirements: [Requirement]
}
struct Badge: Hashable, Codable, Identifiable {
let data: BadgeData
var id: Int {
data.id
}
var isTaken: Bool
}
struct Requirement: Hashable, Codable, Identifiable {
var id: Int
let title: String
let detail: String
}