I'm creating a view by reading several json files at once. I'm reading 6 files from indexes 0 to 5 and displaying each view. Until the day before yesterday, only the view of index 0 was executed and the rest of the errors appeared, but I didn't correct anything, but the file in index 1 starts to read, and from 2 the same error appears. In my json file, there is no '/' in row 1 of column 1, but there is a '/' in row 1 of column 1, so I think they say that they can't read it as a json file
[
{
"name": "recipe1", "id": 1001, "description": "1", "imageName": "Recipe01"
},
{
"name": "recipe2", "id": 1002, "description": "2", "imageName": "Recipe01"
},
{
"name": "recipe3", "id": 1003, "description": "3", "imageName": "Recipe01"
},
{
"name": "recipe4", "id": 1004, "description": "4", "imageName": "Recipe01"
},
{
"name": "recipe5", "id": 1005, "description": "5", "imageName": "Recipe01"
},
{
"name": "recipe6", "id": 1006, "description": "6", "imageName": "Recip01"
},
{
"name": "recipe7", "id": 1007, "description": "7", "imageName": "Recip01"
}
]
Post
Replies
Boosts
Views
Activity
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.