import Foundation
struct RGBValue: Codable {
let blue, green: Double
let timestamp: String
let red: Double
}
struct RGB: Codable {
let fps: Double
let heartRate: Int
let rgbValues: [RGBValue]
static let rgbData: [RGB] = Bundle.main.decode(file: "hrv_data.json")
static let sampleData: RGB = rgbData[0]
}
extension Bundle{
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Could not find (file) in the project!")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load (file) in the project!")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode (file) in the project! error : (Error.self)")
}
return loadedData
}
}
This is the entire code snippet which shows no error, but while run time it crashes “Thread 1: Fatal error: Could not decode hrv_data.json in the project! error : Error”
How to solve the issue and know the reason behind this error?
Sourabh Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.