Post not yet marked as solved
Post marked as unsolved with 2 replies, 211 views
Hi !
I have this code bellow in my project and I am trying to parse a local JSON file.But it can't get through the line of the if let localData statement. I don't know why.
let id: Int
let title: String
let artist: String
let isOut: String
let label: String
let vinylAudoID: String
let vinylCountry: String
let vinylFormat: String
let vinylID: String
let vinylLocation: String
let year: String
}
class CodableViewModel: ObservableObject {
@Published var vinyl: VinylModel? = nil
init() {
getData()
}
func getData() {
guard let data = getJSONData() else { return }
if let localData = try? JSONSerialization.jsonObject(with: data,options: []),let dictionary = localData as? [String:Any] {
let id = dictionary["id"] as? Int ?? 1
let title = dictionary["title"] as? String ?? ""
let artist = dictionary["artist"] as? String ?? ""
let isOut = dictionary["isOut"] as? String ?? ""
let label = dictionary["label"] as? String ?? ""
let vinylAudoID = dictionary["vinylAudoID"] as? String ?? ""
let vinylCountry = dictionary["vinylCountry"] as? String ?? ""
let vinylFormat = dictionary["vinylFormat"] as? String ?? ""
let vinylID = dictionary["vinylID"] as? String ?? ""
let vinylLocation = dictionary["vinylLocation"] as? String ?? ""
let year = dictionary["year"] as? String ?? ""
print(title)
}
}
func getJSONData() -> Data? {
if let url = Bundle.main.url(forResource: "*****", withExtension: "json"),let data = try? Data(contentsOf: url) {
return data
} else {
print("DEU RUIM")
return nil
}
}
}
any ideas why this is happening? If I put those if lets in separated lines it enters the if let localData but it does not enter the if let dictionary = localData as? [String:Any]
thank u