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

SOLVED: Coming from C to Swift Debugging help

Forums > 100 Days of SwiftUI

Hi

I am new to Swift and have decided to try to learn it. I have been following along with the course and I have been typing in the code as it is presented but I get an error with the let menu = Bundle....yada yada. I get three errors when compiling. I have physically typed it and I have cut and pasted it and get the same results with each method. Any help?

Screen Shot https://imgur.com/a/Ce871Jw

2      

Hi,

Which course are you following?

2      

@roonToon has some issues with the yada yada protocol in SwiftUI.

I have been typing in the code as it is presented but I get an error with the let menu = Bundle....yada yada.

Couple of things.

First, welcome to HackingWithSwift and this wonderful course.

Second, please ask questions. There are many here willing to help.

Next, helpers often try to run your code to find the error. So I offer this critique. I do not want to re-type your code. I prefer to copy/paste your code from this forum into a test application, or a Playground file. Please use the markdown provided and copy your code into your message. Nota bene: We don't need to see ALL THE CODE, just the section giving you issues. Boil the problem down to its bare essence, if you please.

Please add loads of comments to your code. What do you understand? What do you think the code should do? This helps us point you in the right direction, or perhaps, uncover the root cause of your understanding. Yada, yada.

Also, your error code is pretty clear. It seems you are trying to decode some JSON that should resolve into an array of MenuSection objects. Yet, the error code states: Cannot find MenuSection in scope.

Yikes! How is Swift supposed to decode JSON into chunks of MenuSections if you're not including MenuSection where Swift can find it?

What day are you on with your 100 Days of SwiftUI journey? Please tell us what day you are on, so we can point you to the lesson that you need to revisit and study!

Good luck

Keep Coding!

2      

From this tutorial. Building a menu using List

This is the line I am getting the errors on.

let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")

The errors are.

Cannot convert value of type '[Any]' to expected argument type 'T.Type'

Cannot find 'MenuSection' in scope

Generic parameter 'T' could not be inferred

//
//  ContentView.swift
//  HackingSwift
//
//  Created by Roontoon on 5/7/23.
//

import SwiftUI

struct ContentView: View {

  //let menu = Bundle.main.decode([MenuSection].self, from: "menu_json")
    let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")
        var body: some View {
        VStack {
            NavigationStack{
                List{
                    Text("Hello World")
                    Text("Hello World")
                    Text("Hello World")
                }.navigationTitle("Menu")
            }

        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

Now we're getting somewhere!

This line says "I have a file of JSON and I want to turn it into an array of MenuSection objects.

let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")  // <-- Turn JSON into array of MenuSection objects.

Also, you noted the lesson: Building a menu using List

Here's an important line from that lesson.

If you open Menu.swift you’ll see structs that define MenuSection and MenuItem,

I don't think you have Menu.swift in your solution. You are missing these key structures. Where is your definition of MenuSection? This is probably a struct?

3      

@Obelix

That was the issue. Forgot to copy menu.swift into my project. Thanks

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.