TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: CoreDataProject question

Forums > 100 Days of SwiftUI

Hello,

On CoreDataProject, when I get to the part with code:

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc

    @FetchRequest(sortDescriptors: []) var wizards: FetchedResults<Wizard>

    var body: some View {
        VStack {
            List(wizards, id: \.self) { wizard in
                Text(wizard.name ?? "Unknown")
            }

            Button("Add") {
                let wizard = Wizard(context: moc)
                wizard.name = "Harry Potter"
            }
            Button("Save") {
                do {
                    try moc.save()
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }
}

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

I keep getting the following errors: "Cannot find type "Wizard" in scope" "Unable to infer type of a closure parameter for 'wizard' in the current context"

Because of the errors, I'm not able to run the code.

I've started this project from the beginning 3 times now to try to find errors from my end but I cannot. Will appreciate it if someone will point me in the correct direction so that I can move on with the project.

Thank you

2      

I tried using :

@FetchRequest(entity: Wizard.entity(), sortDescriptors: []) var wizards: FetchedResults<Wizard>

but still have the same error. I don't see it in Paul's tutorial anywhere. Any other ideas?

2      

In your CoreData Model under the Wizard entity. What did you select for CodeGen? It should be Class Definition for this example.

2      

I solved the issue. The problem was that I dragged & dropped the DataController file directly from the Bookworm app into the CoreDataProject app.
Even though I changed the NSPersistentContainer name to "CoreDataProject", for some reason it created a problem.
I finally deleted the DataController and made a brand new one and the problem disappeared.

Thank you all for the ideas.

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.