29.5 Loading the JSON Data
The project is also going to need a way to load the carData.json file and translate the car entries into an array of Car objects. For this we will add another Swift file containing a convenience function that reads the JSON file and initializes an array which can be accessed elsewhere in the project.
Using the steps outlined previously, add another Swift file named CarData.swift to the project and modify it as follows:
import UIKit
import SwiftUI
var carData: [Car] = loadJson("carData.json")
func loadJson<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename,
withExtension...