here is my code.
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 bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) from bundle.")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode \(file) from bundle.")
}
return loadedData
}
}
I am having issues on these lines.
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) from bundle.")
}
Someone please help. Thank you!
Which line exactly (of the 2 you quoted) is producing the error message?
One possible cause for weird errors like this is if you've created your own type called Data
. This conflicts with the standard library Data
type, and can result in some very odd messages.
The above code compiles without errors for me, BTW.