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

Project 11 (Bookworm) problems

Forums > 100 Days of SwiftUI

Hi, stuck on a problem here - help welcome.

I've worked through the example step by step to build the Bookworm app, got as far as "Creating books with Core Data". The code won't compile though.

I've tried on two different Macs (rebooted) and I've also tried the completed tutorial code as well as the partial example I've built up. Won't work either way.

The compiler error messages I'm getting (from the completed Project 11 tutorial) are: 1) ContentView: "Use of undeclared type 'Book' 2) AddBookView: "Use of unresolved identifier 'Book' 3) DetailView: both of the above

Any insights?

Thanks

3      

Might help to add some code to your post!

3      

I've pasted in some of the code below for ContentView of Project 11 (Bookworm) - this is from the tutorial files downloaded from GitHub at https://github.com/twostraws/HackingWithSwift. This particular project is in the SwiftUI section.

The Buildtime error message shows up at @FetchRequest and says "Use of undeclared type 'Book'. I've checked that Book is defined as an entity in the xcdatamodeld file.

// // ContentView.swift // Project11 // // Created by Paul Hudson on 17/02/2020. // Copyright © 2020 Paul Hudson. All rights reserved. //

import SwiftUI

struct ContentView: View { @Environment(.managedObjectContext) var moc @FetchRequest(entity: Book.entity(), sortDescriptors: [ NSSortDescriptor(keyPath: \Book.title, ascending: true), NSSortDescriptor(keyPath: \Book.author, ascending: true) ]) var books: FetchedResults<Book>

@State private var showingAddScreen = false

var body: some View {
     NavigationView {
        List {
            ForEach(books, id: \.self) { book in
                NavigationLink(destination: DetailView(book: book)) {
                    EmojiRatingView(rating: book.rating)
                        .font(.largeTitle)

                    VStack(alignment: .leading) {
                        Text(book.title ?? "Unknown Title")
                            .font(.headline)
                        Text(book.author ?? "Unknown Author")
                            .foregroundColor(.secondary)
                    }
                }
            }
            .onDelete(perform: deleteBooks)
        }
        .navigationBarTitle("Bookworm")
        .navigationBarItems(leading: EditButton(), trailing: Button(action: {
            self.showingAddScreen.toggle()
        }) {
            Image(systemName: "plus")
        })
        .sheet(isPresented: $showingAddScreen) {
            AddBookView().environment(\.managedObjectContext, self.moc)
        }
    }
}

etc

3      

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!

Sorry, I applied some formatting to the problematic line of code and that messed things up - see all original code in ContentView below...

// // ContentView.swift // Project11 // // Created by Paul Hudson on 17/02/2020. // Copyright © 2020 Paul Hudson. All rights reserved. //

import SwiftUI

struct ContentView: View { @Environment(.managedObjectContext) var moc @FetchRequest(entity: Book.entity(), sortDescriptors: [ NSSortDescriptor(keyPath: \Book.title, ascending: true), NSSortDescriptor(keyPath: \Book.author, ascending: true) ]) var books: FetchedResults<Book>

@State private var showingAddScreen = false

var body: some View {
     NavigationView {
        List {
            ForEach(books, id: \.self) { book in
                NavigationLink(destination: DetailView(book: book)) {
                    EmojiRatingView(rating: book.rating)
                        .font(.largeTitle)

                    VStack(alignment: .leading) {
                        Text(book.title ?? "Unknown Title")
                            .font(.headline)
                        Text(book.author ?? "Unknown Author")
                            .foregroundColor(.secondary)
                    }
                }
            }
            .onDelete(perform: deleteBooks)
        }
        .navigationBarTitle("Bookworm")
        .navigationBarItems(leading: EditButton(), trailing: Button(action: {
            self.showingAddScreen.toggle()
        }) {
            Image(systemName: "plus")
        })
        .sheet(isPresented: $showingAddScreen) {
            AddBookView().environment(\.managedObjectContext, self.moc)
        }
    }
}

func deleteBooks(at offsets: IndexSet) {
    for offset in offsets {
        // find this book in our fetch request
        let book = books[offset]

        // delete it from the context
        moc.delete(book)
    }

    // save the context
    try? moc.save()
}

}

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

3      

Obviously it can't seem to find your type Book. Can have various reasons. Try and clean your project: CMD-SHIFT-K and then build your project: CMD-B

Does that help? If not, not a typo somewhere ?

3      

Thanks, I'd already tried cleaning and rebuilding and still got the same problem.

The project is directly from Paul's Hacking With Swift(UI) course so probably the simplest thing would be for me to message him directly in a day or so if no-one on the forum has had this issue and found a solution.

3      

I've copied your code and used in my project, the only problem I got is in this line: @Environment(.managedObjectContext) var moc

You need to add backslash in order to compile. @Environment(\.managedObjectContext) var moc

But actually for this one I'm getting different error, Generic parameter 'Value' could not be inferred.

3      

If your first bit of code that you did not put in codeformat, looks like this

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Book.entity(), sortDescriptors: [
        NSSortDescriptor(keyPath: \Book.title, ascending: true),
        NSSortDescriptor(keyPath: \Book.author, ascending: true)
    ]) var books: FetchedResults<Book>

then check that when click on Book ENTITIES and check the right hand panel the Entity Name has Book, Class Name has Book and Codegen is Class Definition

Did you set up project with the Core Data ticked. If not you will have to add some code in AppDelegate.swift and SceneDelegate.swift

3      

Thanks, I've confirmed that the entity name, class name and codegen are as you said.

Also, the project was set up with Core Data ticked.

Sadly, the problem remains.

3      

Sorry to hear that I was trying to create the errors and when changed these fields give me the error you got. I hope that you get an answer.

3      

I had the same error - with the “How you combine the Core Data and Swift UI" section in Bookworm while replicating Paul's student example. In the end fully closing Xcode then restarting it solved the problem. – Hope this helps.

3      

I've just been trying to solve this myself, and what worked for me was opening the Inspector for Bookworm.xcdatamodeld and unchecking then checking the checkbox for Bookworm under Target Membership. Hope this helps anyone else running into the issue.

3      

I just stumbled upon a weird bug (tested in Xcode 11.1 and 13.1 in Mojave). If the project name has a space dash space, then the project won't build. For example, I've been naming my projects like this: Project 11 - Bookworm. No matter what, it won't build. When I take out a space on either side of the dash in a new project, then it would work. Granted, I still needed to do the clean/restart Xcode/build dance, but at least it would compile after that. Hope that helps anyone that may have trouble with Core Data.

3      

Seems a similar problem as dicussed in https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-53-bookworm-use-of-undeclared-type-student-error-when-using-core-data/1265 Try the solution byaruhaf proposed in that thread.

3      

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!

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.