NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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
        }
    }
}

1      

What's the error?

1      

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

1      

Change:

struct Bundle {

to:

extension Bundle {

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

1      

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

1      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to try Swift on microcontrollers? MadMachine provides ways to interact with the physical world in a Swift way. Join us and have fun!

Get it now

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

Reply to this topic…

You need to create an account or log in to reply.

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.