In SwiftUI I want to show the favourite items only and I'm trying to do this with an if statement within the ForEach-element. See the code:
var body: some View {
NavigationView {
List {
Toggle(isOn: $showFavoritesOnly) {
Text("Favorites only")
}
ForEach(self.buildings, id: \.self) { building in
if(!self.showFavoritesOnly || building.favourite) {
Text("Blah")
}
}
}
}
}
However I getting a 'Unable to infer complex closure return type; add explicit type to disambiguate' error.
How can I select an item conditionally in a ForEach element?
ForEachis asking for theelsepart as it can not return a nilViewif condition does not match. Just a suggestion I am not sure!