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

SOLVED: Moonshot project - Error in Xcode

Forums > 100 Days of SwiftUI

Hello all, I am following the lesson of the day 40. And I have an Xcode error that won't let me compile the code. Compared to Paul's code, I don't see any difference. Has this happened to anyone else or do you know how to solve it? Thank you very much!!

struct Bundle {
    func decode(_ file: String) -> [String: Astronaut] {
        guard let url = self.url(forResource: file, withExtension: nil) else { //Error here: 'nil' requires a contextual type - and: Value of type 'Bundle' has no member 'url'
            fatalError("Failed to locate \(file) in bundle.") // Locate JSON
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.") // Load JSON
        }

        let decoder = JSONDecoder()

        guard let loaded = try? decoder.decode([String: Astronaut].self, from: data) else {
            fatalError("Failed to decode \(file) from bundle.") // Decode JSON
        }
    }
}

2      

What's the error?

2      

I have pasted the code now because the image I had uploaded has not loaded. Thanks!

2      

Change:

struct Bundle {

to:

extension Bundle {

because you are extending the existing Bundle type, not declaring a new type called Bundle.

2      

You're right!! I hadn't noticed, thank you very much!

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.