I encountered a "Type of expression is ambiguous without a type annotation" error when building a SwiftUI view with nested view hierarchies and complex conditional expressions. This error appears to be a compiler issue, as it occurs even when the code logic is sound and follows typical SwiftUI patterns. In some cases, the compiler fails to resolve types correctly, even with explicit type annotations.
var listView: some View {
ScrollView {
VStack(spacing: 10) {
ForEach(filteredLists, id: .listId) { list in
Button(action: {
selectListAndNavigate(list)
}) {
GroceryListView(
listName: list.listName,
isHeartSelected: bindingForHeartSelected(at: viewModel.lists.firstIndex(where: { $0.listId == list.listId }) ?? 0),
onCardTapped: {
selectListAndNavigate(list)
}
)
.padding(.horizontal)
}
.buttonStyle(PlainButtonStyle())
}
// NavigationLink outside ForEach to handle navigation
NavigationLink(
destination: ListView(
categories: viewModel.categorizedProducts,
listID: viewModel.currentList?.recordID,
listName: viewModel.listName,
createListViewModel: viewModel,
list: viewModel.currentList
),
isActive: $isNavigatingToList
) {
EmptyView() // Invisible trigger for navigation
}
}
}
}