import Foundation
var Dietary0: [DietaryTypeList0] = load("RecipList0.json")
var Dietary1: [DietaryTypeList1] = load("RecipList1.json")
var Dietary2: [DietaryTypeList2] = load("RecipList2.json")
var Dietary3: [DietaryTypeList3] = load("RecipList3.json")
var Dietary4: [DietaryTypeList4] = load("RecipList4.json")
var Dietary5: [DietaryTypeList5] = load("RecipList5.json")
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
This is my ModelData File
I want to use multiple json files to decode according to the index, but FatalError pops up. When I opened a new file, I succeeded in decoding two json files into one model data file. I want to know how to decode 6 json files with one model data file or use multiple model files.