Argument passed to call that takes no arguments help

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!

Answered by DTS Engineer in 758582022

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.

Accepted Answer

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.

The line that says

guard let data = try? Data(contentsOf: url) else {

I might be possible that using data wont work.

nvm, I looked through my code and found the issue, I had "Data" as one of my structs. Thank you for the help though!

Argument passed to call that takes no arguments help
 
 
Q