 
     func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
 
-        updateSearchResultsForSearchController(searchController)
-    }
-}
-
+        if let searchController = searchController {
+            updateSearchResultsForSearchController(searchController)
+        }
+    }
+}
+


import UIKit
import RealmSwift

class ContactsViewController: BaseViewController {

    @IBOutlet weak var contactsTableView: UITableView!

    var searchController: UISearchController!

    let cellIdentifier = "ContactsCell"


extension ContactsViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (searchController?.active ?? false) ? Int(filteredFriends.count) : Int(friends.count)
    }

    private func friendAtIndexPath(indexPath: NSIndexPath) -> User? {
        let index = indexPath.row
        let friend = (searchController?.active ?? false) ? filteredFriends[safe: index] : friends[safe: index]
        return friend
    }

