CodexBloom - Programming Q&A Platform

SwiftUI List implementation guide after editing item in iOS 17

πŸ‘€ Views: 0 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-14
swift swiftui ios17 Swift

I'm not sure how to approach I'm confused about I'm having an scenario with a SwiftUI `List` not updating correctly after I modify an item in my data model. I'm using iOS 17 and I have a view that displays a list of items, each of which can be edited. When I edit an item, it doesn't update the view to reflect the changes until I navigate away and come back. Here’s the relevant code snippet: ```swift struct Item: Identifiable { let id: UUID var name: String } class ItemViewModel: ObservableObject { @Published var items: [Item] = [] func updateItem(at index: Int, with newName: String) { items[index].name = newName // I expected the view to update here automatically } } struct ItemListView: View { @ObservedObject var viewModel: ItemViewModel var body: some View { List { ForEach(viewModel.items) { item in Text(item.name) .onTapGesture { // Code to edit the item viewModel.updateItem(at: index, with: "New Name") } } } } } ``` I've confirmed that `updateItem` is being called and that the `items` array is being modified. However, the UI does not reflect these changes immediately. I also checked that `Item` conforms to `Identifiable` and `ItemViewModel` to `ObservableObject`, so it should be notifying the view correctly. What could I be missing here, or is there a specific condition in iOS 17 related to SwiftUI that I need to be aware of? Has anyone else encountered this? I'd really appreciate any guidance on this. I've been using Swift for about a year now. Has anyone dealt with something similar? The project is a mobile app built with Swift. Has anyone dealt with something similar?