I make a call to a food api that has a query based off user input.
How can I detect if a user enters a space in their search (ie. if they search for a word like "Whey Protein".
func searchFood(userItem: String){
//calls api search
guard let url = URL(string: "https://api.nal.usda.gov/fdc/v1/foods/search?&api_key=bRbzV****y8cqG&query=\(userItem)") else {return}
URLSession.shared.dataTask(with: url) { (data, _,_) in
let searchResults = try! JSONDecoder().decode(APISearchResults.self, from: data!)
DispatchQueue.main.async {
for item in searchResults.foods ?? []{
self.foodDescription = item.lowercaseDescription?.firstCapitalized ?? "food not valid"
self.calories = String(Double(round(item.foodNutrients?[3].value! ?? 0.00)).removeZerosFromEnd())
self.brand = item.brandOwner ?? "General"
}
}
}
.resume()
}