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

extension ContactsViewController: UITableViewDataSource, UITableViewDelegate {

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

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



import UIKit
import RealmSwift
import Ruler

class ContactsViewController: BaseViewController {

    @IBOutlet weak var contactsTableView: UITableView!

    var searchController: UISearchController?

    let cellIdentifier = "ContactsCell"

