I'm trying to add a search bar to the top of a grouped table. However I'm unsure how to filter through my data. The data is stored in a nested array with objects held in this format;
struct Group {
var id: String
var type: String
var desc: String
var avatar: String
var name: String
init() {
id = ""
type = ""
desc = ""
avatar = ""
name = ""
}
}
Because I get data from two sources, two arrays are nested together, this also makes it simpler to create the two sections of the grouped table. I'll note, they both use the same Group struct.
self.masterArray = [self.clientArray, self.departmentArray]
This "masterArray" is then used to populate the table. Filtering/searching a single array isn't too difficult, but how do I search through a nested array?
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
}
EDIT: I've finally got things working, courtesy of @appzYourLife.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("Searching for:" + searchText)
if searchText.isEmpty {
filterArray = masterArray
} else {
filterArray = [ clientArray.filter { $0.name.range(of: searchText) != nil }] + [ departmentArray.filter { $0.name.range(of: searchText) != nil } ]
}
tableView.reloadData()
}

.filter{ ... }closure in its current form contains an incorrect argument type: the closure expects elements that are arrays themselves ([Group]elements). Try exchanging the above tofilterArray = masterArray.flatten().filter(..... (Or, just:filterArray = masterArray.flatten().filter { $0.id.lowercaseString.containsString(searchText.lowercaseString) }). Also, have a look at this tutorial.filterArrayis[[Group]], in which case the filtering operation must yield an object of type[[Group]]. TryfilterArray = masterArray.map { $0.filter { $0.id.lowercaseString.containsString(searchText.lowercaseString) } }.