Omid
Omid

Reputation: 178

Search Bar in a TableView with Core Data (using Swift)

I am making a TableViewController with Core Data. In fact the users can add new items to the Table View and these items are saved in Core Data. Everything has worked fine but when I add a Search Bar I'm blocked.

I added a Search Bar but this function bellow create an error when I run the app in the simulator.

func updateSearchResultsForSearchController(searchController: UISearchController,)
{
    filteredTableData.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
    let array = (series as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableData = array as! [String]

    self.tableView.reloadData()
}

The error I get is " terminating with uncaught exception of type NSException "

PS : I use Xcode 6.3.2 and all the code is in Swift.

Upvotes: 1

Views: 4026

Answers (1)

Stuart Breckenridge
Stuart Breckenridge

Reputation: 1903

Try the following:

func updateSearchResultsForSearchController(searchController: UISearchController) {
    var request = NSFetchRequest(entityName: "Serie")
    filteredTableData.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF.infos CONTAINS[c] %@", searchController.searchBar.text)
    let array = (series as NSArray).filteredArrayUsingPredicate(searchPredicate)

    for item in array
    {
        let infoString = item.infos
        filteredTableData.append(infoString)
    }

    self.tableView.reloadData()
}

Upvotes: 4

Related Questions