Reputation: 526
I want to filter an array which contains an array of Strings.
My code is:
if(!(searchString?.isEmpty)!) {
shouldShowSearchResults = true
// Filter the data array and get only those countries that match the search text.
filteredPlazaDictionary = plazaDictionary.filter({ (match) -> Bool in
let matchText: NSString = match[1] as NSString
return (matchText.range(of: searchString!, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
})
}
Here, filteredPlazaDictionary[[String]]
and plazaDictionary[[String]]
and I want to match every array[1] inside plazaDictionary
with searchString. Help please.
Upvotes: 0
Views: 608
Reputation: 77621
I would write this something like this I think...
// make it a function that takes everything it needs and returns a result array
func filteredCountryArray(countryArray: [[String]], searchString: String) -> [[String]] {
guard !searchString.isEmpty else {
// search string is blank so return entire array...
return countryArray
}
// Filter the data array and get only those countries that match the search text.
return countryArray.filter { match in
// check array is long enough to get [1] out of it
guard match.count >= 2 else {
return false
}
let matchText = match[1]
return matchText.range(of: searchString, options: .caseInsensitive) != nil
}
}
plazaDictionary
is a very odd name to give to an array. It is an array, not a dictionary.
What you should probably do here is create some data objects (structs, classes, enums, etc...) that hold the data in a better format than a nested array.
This should do what you want for now though.
EDIT
Having another think about it, I would also change the serachString and the array into input parameters...
Upvotes: 3