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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.