Search code examples
swiftswiftuiswiftui-list

How do I use the new binding in List with Swift 5.5?


I'm trying out the new feature in SwiftUI 3 (or, rather Swift 5.5), where you can have direct bindings to items in a List. Should be simple, but I can't grasp it.

Xcode beta 4.

I can click on an item on the list, thereby activating the TextField. However, after entering one character, the TextField loses focus.

Simple example, I tried running it on both MAC and iOS, with the same result.

struct ContentView: View {
    
    @State private var items = ["Item A", "Item B", "Item C"]
   
    var body: some View {
        List ($items, id: \.self) { $item in
            TextField("EditItem", text: $item)
        }
    }
}

Solution

  • You are identifying the elements in the ForEach by using \.self so directly when you edit a string it gets a new identity since it is changed and then SwiftUI sees it as a delete of the object and insert of a new object rather than an update of the existing one.

    When I try this with a struct that conforms to Identifiable it works fine because then SwiftUI can keep track of the changed object using the id property.

    struct Item: Identifiable {
        let id = UUID()
        var name: String
    }
    
    struct ContentView: View {
        @State private var items = [Item(name: "Item A"), Item(name:"Item B"), Item(name: "Item C")]
    
        var body: some View {
            VStack {
                List($items, id: \.id) { $item in
                    TextField("EditItem", text: $item.name)
                }
                Text(items.map(\.name).joined(separator: " : "))
            }
        }
    }
    

    Note that this has most likely nothing to do with SwiftUI 3.0 or any beta but is more of an expected behaviour