Ad

Im Attempting To Create A Search Page For Users To Search Other Users Throughout The Database, But App Keeps Crashing

When i press the button to get to my search page the app crashes and returns "libc++abi.dylib: terminating with uncaught exception of type NSException" I'm still new to coding and I'm not sure what I'm doing wrong

 @IBOutlet weak var SearchTable: UITableView!

    var userArray = [NSDictionary?]()
    var filteredUsers = [NSDictionary?]()

    var dataRef = Database.database().reference()

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar

        dataRef.child("users").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in

            self.userArray.append(snapshot.value as? NSDictionary)

            self.SearchTable.insertRows(at: [IndexPath(row: self.userArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)


        }) { (error) in
            print(error.localizedDescription)
        }

    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if searchController.isActive && searchController.searchBar.text != "" {
            return filteredUsers.count
        }
        return self.userArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath)

        let user : NSDictionary?

        if searchController.isActive && searchController.searchBar.text != "" {
            user = filteredUsers[indexPath.row]
        } else {
            user = self.userArray[indexPath.row]
        }

        cell.textLabel?.text = user?["username"] as? String



        return cell
    }

    func updateSearchResults(for searchController: UISearchController) {

        filterContent(searchText: self.searchController.searchBar.text!)

    }

    func filterContent(searchText:String)
    {
        self.filteredUsers = self.userArray.filter{ user in

            let Uname = user!["username"] as? String

            return(Uname?.lowercased().contains(searchText.lowercased()))!

        }

        tableView.reloadData()
    }



}

I'm expecting for the page to open and i have the ability to search with the search bar. the error message i receive is libc++abi.dylib: terminating with uncaught exception of type NSException

Ad

Answer

This problem has nothing to do with Firebase. From the comment you have written under your question, you pasted this console message instantiated view controller with identifier "UIViewController-vhH-By-pea" from storyboard "Main", but didn't get a UITableView.'. I believe you have hooked up a UIViewController screen from the Storyboard with a UITableViewController class. This is wrong, you need to change your class to be a subclass of UIViewController instead of UITableViewController. Then, set the SearchTableView's delegate and dataSource to it.

So, your class should be:

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, ... {

Then set the TableView's delegate and dataSource to self in the viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    // ...
    SearchTableView.dataSource = self
    SearchTableView.delegate = self
    // ...
}

By the way, if your Controller only have a UITableView in it and nothing else, then, you may want to consider using a UITableViewController instead of a UIViewController with a UITableView. UITableViewController is esseentially a UIViewController with a UITableView being the main view on it, so no need to redo all the work :)

Ad
source: stackoverflow.com
Ad