UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

need to find out what is wrong with my JSON

Forums > Swift

I am creating a multiple choice quiz with a JSON file I created. I took some code from the 100 days of SwiftUI tutorial to decode it. There is obviously something wrong with my JSON because the fatal error is thrown. How can I find out what the error is with my JSON? The code I used is below: 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
}

2      

Please post a sample of your JSON and the data structure you are trying to decode it into.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.