|
Hi all I'm learning Swift/SwiftUI, and have used a few of Paul's tutorials. The relevant one now is this one: https://www.hackingwithswift.com/quick-start/swiftui/how-to-configure-core-data-to-work-with-swiftui I've created the PersistenceController, and that's working fine. It looks like this:
My ContentView has a TabView containing 4 tabs.
The ListView() looks like this:
I now want to make a ListEditDetail view, which is simple enough. I want to click on the item in myList, which will pass the MyList in to the view. This will look like this:
And this is where I'm stuck. I can't work out how to pass the preview data from the PersistenceController in to the Preview here. Any help apprecated |
|
@sparrow is stuck!
Because you made the To access the
@twoStraws notes this is a best practice even with simple structs.
Hacking With Swift ArticleOf course @twoStraws addresses this in a nice article. Grab a hot cuppa, get cozy, and have a good read. See -> Static Examples |
|
Apologies for the slow reply, I didn't get a notification about the reply. Is if possible to subscribe to posts to be notified if someone updates it? I've updated the ListDetailView as you suggested:
However, as I've created myList as a binding, I get the following error:
This is on the last line of the Previews method. I'm trying to pass the MyList in from the ForEach loop on the ListView. I think I'm mixing up State and Binding declarations. ETA: I understand that the preview static is a PersistenceController. What I can't work out is how to extract the myList from that. |
|
|
|
You need a seperate preview for MyList. When working with CoreData and when you want previews for several different entities you can't just use the PersistanceController. You need to create a preview which returns the MyList object. This can result in that you have to create many different objects before you get your desired preview. But all have in common that they must be created in the same Please bear in mind that every entity of CoreData is an ObservableObject. So @Binding doesn't work. You need @ObservedObject for working with entities. |
|
Thanks @Hatsushira The key bit of information I was missing was that I had to instantiate an instance of MyList. Once I figured that out, it was simple enough to do what I needed to. |
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.
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.