Quick Tip Icon
Quick Tip

Automatically generated List edit operations

Starting from iOS 16 and macOS 13 SwiftUI List view can automatically generate move and delete operations without the need of onMove and onDelete closures.

To create a simple editable list, we can pass a binding to a collection to the List view and provide a set of edit operations we want to allow.

struct ContentView: View {
    @State var items = ["A", "B", "C"]
    
    var body: some View {
        NavigationView {
            List(
                $items, id: \.self,
                edits: [.delete, .move]
            ) { $item in
                Text(item)
            }
        }
    }
}

You can see the available options in the documentation for EditOperations.

If we don't want to allow deletion of certain rows, we can use deleteDisabled() modifier on a per row basis.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Level up your Swift skills!$35

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms

Level up your Swift skills!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35