GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

@kassi  

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 try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket 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.