Zebs asks an open ended question, without providing a thorough business case.
How could I use this or similar to delete CoreData objects using a button in a ForEach?
This is a multi-level question. Perhaps you want to give an example of your intentions?
Here's a short bit I wrote about thinking of ForEach
as a view
building factory.
See-> ForEach Button Factory
In summary, you have a collection of some sort. You grind it through a ForEach
factory, and SwiftUI will build you a number of views
. The views can be Images
and Text
. The views can be emoji filled strings
. The views could be HStacks
with Text
and Buttons
side-by-side. Your choice! Most excellent!
So review your business case. You must have a collection of managed CoreData
objects. Call the collection tortoises
. The collection is made of one, perhaps many tortoise
objects.
Well, it's trivial to run these objects through a ForEach
view factory and build custom, cool looking buttons
using the data in the tortoise object. That's essentially the job of the ForEach
view builder.
I think your question becomes: What is the function each button
should perform when tapped
? I suppose you want to delete that managed object from the collection? Go for it!
Come back and let us know how you solved this!