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

Swiping actions on NSManagedObject list

Forums > SwiftUI

Hello again. Two questions:

  1. I'm trying to add swipe actions to my lists of NSManaged Objects (as Edit() "default" option only gives the option for trailing deletion), but I can't find the way to pass "autor" from "ForEach" closure out to swipeActions modifier: it says "Cannot find 'autor' in scope".

  2. By the way, for expert SwiftUI programmers: how can I use this swipe option to edit elements from the list, just in place if possible :-) , taking into account that all of them are NSManagedObjects?

Thank you very much in advance :-)

struct MantenimientoAutoresView: View {

    @Environment(\.managedObjectContext) var moc
    @FetchRequest(sortDescriptors: [SortDescriptor(\.nombre)]) private var autores: FetchedResults<Autor>

    var body: some View {

        List {
            ForEach(autores, id: \.id) { autor in
                Text(autor.nombre ?? "")
            }

            .swipeActions(edge: .trailing) {
                moc.delete(autor)

                }
            }
            // More code below...

2      

Your swipeAction is bound to the ForEach, i.e. outside of your closure that gets the autor variable. Try moving it to the Text.

In addition, if you want to be able to delete any eleent in the list, consider using and implementing the onDelete modifier on the ForEach.

2      

Yes! What an idiot of mine :-))) Thanks!

About my second question? Any idea? I'm now using this option to access my CoreData objects as Binding ones, in order to load its data the same time I have the option to edit them:

https://stackoverflow.com/questions/59001253/how-should-i-bind-to-a-core-data-entity-string-attribute-within-a-swiftui-view

It works, but I can't use it for in-place-editing on lists, forms or pickers. Even using this sintax:

TextField(Binding(autor.nombre, replacingNilWith: ""))

2      

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.