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?

0

1 Answer 1

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: [""])]

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

2 Comments

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. stackoverflow.com/questions/46854927/…
i try to answer your both questions

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.