Hi community,
Sorry for the basic question, but since yesterday I am not able to decode this JSON data. I have implemented the Bundle-Decodable from the Moonshot project.
Getting this error: Fatal error: Failed to decode scales.json from bundle.
[
{
"id": "pA",
"scales": [
{
"activity" : "Legato Pentascales",
"key": "C, G, D, Am",
"played": "Hands Separated",
"playedDesc": "Tonic to dominat, ascending and descending, ending with solid root-position triad",
"tempoValue": "100",
"noteValue:": "quarter"
},
{
"activity" : "Staccato Pentascales",
"key": "C, G, D, Am",
"played": "Hands Separated",
"playedDesc": "Tonic to dominat, ascending and descending, ending with solid root-position triad",
"tempoValue": "100",
"noteValue:": "quarter-staccato"
}
]
},
{
"id": "pB",
"scales": [
{
"activity" : "Legato Pentascales",
"key": "D, A, F, Em, Dm",
"played": "Hands Separated",
"playedDesc": "Tonic to dominat, ascending and descending, ending with solid root-position triad",
"tempoValue": "60",
"noteValue:": "2-eighth"
},
{
"activity" : "Staccato Pentascales",
"key": "D, A, F, Em, Dm",
"played": "Hands Separated",
"playedDesc": "Tonic to dominat, ascending and descending, ending with solid root-position triad",
"tempoValue": "60",
"noteValue:": "2-eighth-staccato"
},
{
"activity" : "One-octave Scales",
"key": "C, G, Am (natural)",
"played": "Hands Separated",
"playedDesc": "One-octave",
"tempoValue": "60",
"noteValue:": "2-eighth"
},
{
"activity" : "Contrary Motion Scale",
"key": "C",
"played": "Hands Together",
"playedDesc": "One-octave",
"tempoValue": "60",
"noteValue:": "2-eighth"
}
]
}
]
This is my struct:
import Foundation
struct TechRequirements: Codable, Identifiable {
let id: String
let scales: [Scale]
}
struct Scale: Codable {
let activity: String
let key: String
let played: String
let playedDesc: String
let tempoValue: String
let noteValue: String
}
Bundle-Decodable:
import Foundation
extension Bundle {
func decode<T: Codable>(_ file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
let decoder = JSONDecoder()
guard let loaded = try? decoder.decode(T.self, from: data) else {
fatalError("Failed to decode \(file) from bundle.")
}
return loaded
}
}
My DataManager:
import Foundation
class DataManager: ObservableObject {
@Published var scales: [TechRequirements] = Bundle.main.decode("scales.json")
}
Thanks for the support.
Renato.