title: Search bar not working in Swift 4 IOS App
body: <p>I have implemented a search bar to search for users, but nothing shows up in the table view when I search for something. I have attached a picture of my view controller below.</p>
  
  <p>View Controller:</p>
  
  <p><img src="https://i.stack.imgur.com/IhWkx.png" alt="View Controller"></p>
  
  <p>This view controller shows a list of all the users and the search bar is supposed to help the user find a username.</p>
  
  <pre><code>import UIKit
  
  class FindFriendsViewController: UIViewController {
  
  var users = [User]()
  
  @IBOutlet weak var tableView: UITableView!
  @IBOutlet weak var searchBar: UISearchBar!
  
  var searchItem = [String]()
  var searching = false
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      tableView.tableFooterView = UIView()
      tableView.rowHeight = 71
  
      let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
      tap.cancelsTouchesInView = false
      self.view.addGestureRecognizer(tap)
  }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
  
      UserService.usersExcludingCurrentUser { [unowned self] (users) in
          self.users = users
  
          DispatchQueue.main.async {
              self.tableView.reloadData()
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>extension FindFriendsViewController: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      if searching {
          return searchItem.count
      } else {
          return users.count
      }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "FindFriendsCell") as! FindFriendsCell
  </code></pre>
  
  <p>//        let user = users[indexPath.row]</p>
  
  <pre><code>    var usernamesArr = [String]()
      for user in users {
          usernamesArr.append(user.username)
      }
  
      if searching {
          cell.textLabel?.text = searchItem[indexPath.row]
      } else {
          cell.textLabel?.text = usernamesArr[indexPath.row]
          cell.delegate = self
          configure(cell: cell, atIndexPath: indexPath)
      }
  
      return cell
  }
  
  func configure(cell: FindFriendsCell, atIndexPath indexPath: IndexPath) {
      let user = users[indexPath.row]
  
      cell.usernameLabel.text = user.username
      cell.followButton.isSelected = user.isFollowed
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>extension FindFriendsViewController: FindFriendsCellDelegate {
  func didTapFollowButton(_ followButton: UIButton, on cell: FindFriendsCell) {
      guard let indexPath = tableView.indexPath(for: cell) else { return }
  
      followButton.isUserInteractionEnabled = false
      let followee = users[indexPath.row]
  
      FollowService.setIsFollowing(!followee.isFollowed, fromCurrentUserTo: followee) { (success) in
          defer {
              followButton.isUserInteractionEnabled = true
          }
  
          guard success else { return }
  
          followee.isFollowed = !followee.isFollowed
          self.tableView.reloadRows(at: [indexPath], with: .none)
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>extension FindFriendsViewController: UISearchBarDelegate {
  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
      var usernamesArr = [String]()
      for user in users {
          usernamesArr.append(user.username)
      }
      searchItem = usernamesArr.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
      searching = true
      tableView.reloadData()
  }
  </code></pre>
  
  <p>}</p>