0

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.

9
  • What does (!(searchString?.isEmpty)!) do? It seems far too complicated. Commented Dec 23, 2016 at 14:30
  • 1
    OK, so what is going wrong with your code? What IS happening that you think is not right? Commented Dec 23, 2016 at 14:31
  • 1
    @thewarri0r9, and crashes everything if searchString happens to be nil Commented Dec 23, 2016 at 14:31
  • 3
    (foo?.bar)! is nonsense, to check for non-nil and non-empty use if let query = searchString, !query.isEmpty { ... (no parentheses at all). Commented Dec 23, 2016 at 14:35
  • 2
    If plazaDictionary is a [[String]], then why is it called plazaDictionary? It's an array, not a dictionary. Commented Dec 23, 2016 at 14:36

1 Answer 1

3

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...

Sign up to request clarification or add additional context in comments.

6 Comments

Somthings strange is happening. I used your code : filteredPlazaDictionary = self.filteredCountryArray(countryArray: plazaDictionary, searchString: searchString!) self.tollBothPlazaTableView.reloadData() I am calling your function like this But when searchString isn't empty cellForRowAt indexPath doesn't get a called. In short tableView does not reload.
Cell for row is a completely unrelated function. Is the array being filtered properly?
Yes it is but if TableView is not reloading then its not helping.
@thewarri0r9 are you telling the table view to reload?
@thewarri0r9 look at your question. Nothing in there mentions anything about table view. This is a question about filtering an array. If you are having trouble elsewhere is nothing to do with this function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.