2

Hello I have a array that has multiple values and I want to try and filter out the index's for my search bar. An Example in English terms would be like. Give me the index for the name "Name2" and do this by checking for all the strings in the first value for each index.

Code:

 // Multiple Errors 

 var receivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4","Grape","Rotten"]]


 filteredData = data.filter({$0 == searchBar.text})
 filteredData = receivedList.filter({$0 receivedList[1] == searchBar.Text})


 //Im not really sure how to use this or if it's even useful 

 let searchPredicate = NSPredicate(format: "Orange CONTAINS[C] %@", searchText)

 let array = (receivedList as NSArray).filtered(using: searchPredicate)

Iv'e checked these pages on here.

filtering-array-of-dictionaries-in-swift

swift-filter-dictionary

filter-array-of-objects-with-multiple-criteria-and-types-in-swift

And a-couple others, with no luck

4
  • There is no dictionary in your code. Only an array of arrays Commented Oct 31, 2017 at 1:35
  • Thanks for the typo I fixed it @Leo Dabus Commented Oct 31, 2017 at 1:37
  • What do you want the output of your code to be? Your question says "filter" but then you're also saying you want to find the index. Commented Oct 31, 2017 at 1:39
  • It would give me the index like it would reload a tableview and then present the new filtered tableview. I know how to do the rest like reload the tableview etc. I do need help on the filtering though @4castle Commented Oct 31, 2017 at 1:41

2 Answers 2

5

If I read the question correctly, you want to find the index of the array who's first element matches a search pattern. The following demonstrates how to do that:

var receivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4","Grape","Rotten"]]
var searchText = "Name2"

let index = receivedList.index { $0[0] == searchText }

print(index)

The following will filter your list to only those whose first element contains the search text:

let matches = receivedList.filter { $0[0].contains(searchText) }

If you want the indexes that match, then you can use:

let matches = receivedList
    .enumerated()
    .filter { $0.1[0].contains(searchText) }
    .map { $0.0 }
Sign up to request clarification or add additional context in comments.

3 Comments

You can use flatMap to avoid iterating the array again essentially filtering and mapping at the same time e.g. receivedList.enumerated().flatMap { $0.1[0].contains(searchText) ? $0.0 : nil }
Wow Perfect Answer!
@callam Yeah, there's a lot of error protection that can be added, depending on what you do or don't know about your dataset.
2
var receivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4","Grape","Rotten"]]


func filter(keyword: String)-> [[String]] {
    return receivedList.filter({ (stringArr) -> Bool in
        for value in stringArr {
            if value.lowercased().contains(keyword.lowercased()) {
                return true
            }
        }
        return false
    })
}


var filtered = filter(keyword: searchBar.text ?? "") //Here you will get filtered values

Comments

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.