|
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 |
|
|
|
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>
etc |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th. 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>
} struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
|
|
|
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. |
|
I've copied your code and used in my project, the only problem I got is in this line:
You need to add But actually for this one I'm getting different error, Generic parameter 'Value' could not be inferred. |
|
If your first bit of code that you did not put in codeformat, looks like this
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 |
|
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. |
|
|
|
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. |
|
I've just been trying to solve this myself, and what worked for me was opening the Inspector for |
|
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. |
|
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. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.