SwiftUI: Handling Drag and Drop to Reorder List with State Issues
I'm reviewing some code and I'm dealing with I'm relatively new to this, so bear with me... I'm working on a personal project and I'm working on a SwiftUI application where I implemented a drag and drop feature to reorder a list of items. However, Iβm working with an scenario where the list does not update correctly after an item has been moved. The items visually rearrange, but the underlying data structure does not seem to reflect these changes, leading to inconsistencies when I try to save the order later. Hereβs a simplified version of my code: ```swift struct Item: Identifiable { var id: UUID = UUID() var name: String } struct ContentView: View { @State private var items: [Item] = [ Item(name: "Item 1"), Item(name: "Item 2"), Item(name: "Item 3") ] var body: some View { List { ForEach(items) { item in Text(item.name) } .onMove(perform: move) } .navigationBarItems(trailing: EditButton()) } private func move(from source: IndexSet, to destination: Int) { items.move(fromOffsets: source, toOffset: destination) } } ``` I expect that when I drag an item to a new position, the `items` array should reflect that change, and I should be able to save the new order. However, after dragging, when I print the `items` array, it shows the original order. Iβve also tried updating the `@State` declaration and ensuring the `move` function is being called correctly, but nothing seems to solve the question. Iβm running this on Xcode 14.1 and targeting iOS 16. Any insights on what might be going wrong here? My development environment is macOS. What's the best practice here? Any ideas how to fix this? For reference, this is a production CLI tool. I'd really appreciate any guidance on this. I'm coming from a different tech stack and learning Swift. Any help would be greatly appreciated!