0

I am using the following array structure to create a tableView with sections

struct words {

    var sectionName : String!
    var coptic : [String]!
    var english : [String]!
}

var array = [words]()
var filtered = [words]()

array = [words(sectionName: "", coptic: [""], English: [""])]

I want to utilize a searchcontroller using similar code to this

func updateSearchResults(for searchController: UISearchController) {
    // If we haven't typed anything into the search bar then do not filter the results
    if searchController.searchBar.text! == "" {
        filtered = array
    } else {
        // Filter the results
        filtered = array.filter { $0.coptic.lowercased().contains(searchController.searchBar.text!.lowercased()) }

    }

Unfortunately, because coptic is a [String], and not simply a String, the code doesn't work. Is there a way to modify this to be able to filter a search for the coptic subsection?

Mina Makar
  • 51
  • 10

1 Answers1

1

you can do like this.

func updateSearchResults(for searchController: UISearchController) {
    // If we haven't typed anything into the search bar then do not filter the results
    if searchController.searchBar.text! == "" 
    {
        filtered = array
    }
    else 
    {
        filtered.removeAll()
        array.forEach({ (word:words) in

            var tempWord:words = words.init(sectionName: word.sectionName, coptic: [""], english: [""])
            let copticArray = word.coptic.filter({ (subItem:String) -> Bool in
                    let a = subItem.lowercased().contains(searchController.searchBar.text!.lowercased())
                    return a;
                })
            tempWord.coptic = copticArray
            filtered.append(tempWord)
        })

   }
}

Input array = array = [words(sectionName: "abc", coptic: ["apple","ball","cat","dog"], english: [""])]

Search For "app"

OUTPUT words(sectionName: abc, coptic: ["apple"], english: [""])]

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • I asked the same question before and no one responded. I'm attaching a link with the full code I am using. If you can provide me any assistance I would be most grateful. Thank you. https://stackoverflow.com/questions/46854927/uisearchbar-of-an-string-within-an-array – Mina Makar Oct 27 '17 at 19:36
  • 1
    i try to answer your both questions – Jaydeep Vyas Oct 28 '17 at 04:34