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

I'm trying to add CoreData to my SwiftUI project, but I keep getting an error when I render for previews, and when I got to run the app.

Forums > SwiftUI

Hi Everyone, I've been struggling with this for quite a while now but I can't seem to figure this one out. I've searched online on SO, here, and the apple forums, but I can't seem to get past this.

As I've mentioned I'm using CoreData for my back end, with SwiftUI. The core data file has four Entities, with several attributes in each one. They all have property types. Each Entity has also been set to CodeGen: Manual/none.

I can build the project successfully, but whenever I go to run a canvas preview, or build the project it crashes and I get the following errors in a diagnostic report: https://github.com/tylerlw82/errorResponses/blob/main/previewCrash

When I got to run the project I get the following error as an output: https://github.com/tylerlw82/errorResponses/blob/main/outputError

I've been using this as a completed project to reference from. As far as I can tell my project identically mirrors this one, outside of having my own structs and properties etc. https://blckbirds.com/post/core-data-and-swiftui/

I've thought that it might need me to add a preview of placeholder data, but when I do a similar test from the completed project in the tutorial, it seems to be fine and not crash.

I've been at this long enough to know that I'm out of options and really need help. Any advice, feedback or clarification of where I need to go here is greatly appreciated.

Thank you all!

2      

From the look on the error message and both containing

Could not extract a String from KeyPath Swift.KeyPath<RangerCRMSwiftUI.CoreDataAccountModel, Swift.ObjectIdentifier>

I would suggest your KeyPath in your FetchRequest is wrong. Xcode, SwiftUI, CoreData have sometimes problems with the preview, but the error message is the same for both. Perhaps you mispelled your KeyPath in the FetchRequest?

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.