radarsh
(Adarsh)
1
Hi! I am running into the following issue:
The goal of my code is to check a string "text" to see if it contains any variables from a list of allergens (from a different view). If one of the allergens appear in the string, then I want it to appear in the output list. So the user may list a total of 5 allergens, and if only 3 of the allergens are in the "text" string, then those 3 will appear in the output list.
However, I run into the following error: Unable to infer complex closure return type; add explicit type to disambiguate
I understand a lot of other users have had that issue, but I am still unable to figure out how to fix it for my current issue.
I am very new to programming in general, so any help is really appreciated!
var body: some View {
NavigationView {
List {
ForEach(allergens.items) {_ in // The error occurs here!
if allergens.contains(where: self.text.contains){
Text(allergens.items)
}
}
}
}}
Lantua
2
What are the types at each statement?
-
ForEach accepts Array or Range.
-
Text accepts String.
Yet you have allergens.items for both initializers.
Likely it’s a logic error, though I can’t help much after that.
radarsh
(Adarsh)
3
Thanks for your reply!
I actually think I found a workaround:
ForEach(allergens.items) {item in
Text(self.compareResults (x: self.text, y: item.name))
}
Then, outside of the " var body: some View {", I make a function with the following:
func compareResults(x: String, y: String) -> String {
var result = ""
if x.contains(y) {
result = y
}
return result
}
Hope this helps someone else as well!