Hi!
I am having a hard time with this JSON decode and optionals thing.
I have this struct :
var id: Int64?
var title : String?
var artist : String?
var isOut : String?
var label : String?
var vinylAudoID : String?
var vinylCountry : String?
var vinylFormat : String?
var vinylID : String?
var vinylLocation : String?
var year : String?
}
and here is my class to decode a local JSON File that is inside my project.
@Published var vinyls = [Vinyl]()
init() {
loadData()
}
func loadData() {
guard let url = Bundle.main.url(forResource: "vinyls", withExtension: "json") else {
print("deu erro")
return
}
let data = try? Data(contentsOf: url)
let vinyls = try? JSONDecoder().decode([Vinyl].self, from: data!)
self.vinyls = vinyls!
}
}
here is my JSON file:
"vinyls" : [
{
"id" : 1,
"title" : "I’m Missing You",
"artist" : "Fabrica",
"isOut" : "False",
"label" : "Dance Pool",
"vinylAudoID" : "00mwDSOTStNtIMRWIZPs",
"vinylCountry" : "Italy",
"vinylFormat" : "12",
"vinylID" : "DAN6652126",
"vinylLocation" : "LR25",
"year" : "1997"
},
{
"id" : 2,
"title" : "No More, Baby",
"artist" : "Disco Blu",
"isOut" : "False",
"label" : "DJ Approved",
"vinylAudoID" : "02Q6i5tJfUBNEXxanfMM",
"vinylCountry" : "Italy",
"vinylFormat" : "12",
"vinylID" : "APP 9706",
"vinylLocation" : "LR11",
"year" : "1997"
},
{
"id" : 3,
"title" : "Carramba",
"artist" : "Arena Blanca",
"isOut" : "False",
"label" : "Outta Records",
"vinylAudoID" : "02loclZIMqGHzIXI9Xo5",
"vinylCountry" : "Italy",
"vinylFormat" : "12",
"vinylID" : "OTA696004",
"vinylLocation" : "LR19",
"year" : "1996"
}]
}
and here is the error message:
JSON_Parse__vinyl_/ReadData.swift:27: Fatal error: Unexpectedly found nil while unwrapping an Optional value
what is wrong with my code?
thank you very much!