Search code examples
swiftnsarraynspredicateuisearchcontrollerswift-array

Swift - Search using UISearchController and NSPredicate with an array of structs


Currently I'm trying to set up a search bar for my tableView. I want to filter the user's input with NSPredicate to display filtered data from an array of struct objects.

But when I try to call .filteredArrayUsingPredicate() on such an array, I get the following error [Course] is not convertible to 'NSArray'.

Below is my code and a link to the repo. I'm also open to any better methods of approach.

import UIKit
import SwiftyJSON

class CourseTableViewController: UITableViewController, UISearchResultsUpdating {

    var allCourses: [Course] = [Course]()

    var searchArray: [Course] = [Course]() {
        didSet {
            self.tableView.reloadData()
        }
    }

    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()
        retrieveCourses()
        configureView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func configureView() {
        tableView.rowHeight = 50

        // Search Controller Initialization
        self.resultSearchController = UISearchController(searchResultsController: nil)
        resultSearchController.searchResultsUpdater = self
        resultSearchController.hidesNavigationBarDuringPresentation = false
        resultSearchController.dimsBackgroundDuringPresentation = false
        resultSearchController.searchBar.searchBarStyle = .Minimal
        resultSearchController.searchBar.sizeToFit()
        self.tableView.tableHeaderView = resultSearchController.searchBar
    }

    func retrieveCourses() {
        APIService.getAllCourses() { (data) -> Void in
            for courseIndex in data {
                var course: Course = Course(courseJSON: courseIndex.1)
                self.allCourses.append(course)
                println("Course Index: " + String(self.allCourses.count))
            }

            self.sortAllCourses()
        }
    }

    func sortAllCourses() {
        allCourses.sort() {$0.abbr < $1.abbr}
        self.tableView.reloadData()
    }

    // MARK: - Search

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        self.searchArray.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)

        // ERROR is on this line
        let filteredArray: Array = (allCourses as NSArray).filteredArrayUsingPredicate(searchPredicate)

        self.searchArray = filteredArray as [Course]
    }

    // Full tableView code can be found in the repo
}

Link: https://github.com/classmere/app/tree/feature/issue/1/implementSearch

Many Thanks!


Solution

  • So after much looking around, I found that a better approach for a situation like this would be to simply use .filter() and .rangeOfString() to solve this

    theArray.filter() { $0.json?.rangeOfString(searchQuery, options: .CaseInsensitiveSearch) != nil}
    

    While NSPredicate is not used, this appears to be a much easier implementation for my current setup.