title: Getting autocomplete to work in swift
body: <p>I am trying to implement autocompletion, but can't find an example that works in Swift. Below, I'm tring to convert <a href="http://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values">Ray Wenderlich's autocompletion tutorial</a> and example code from 2010. Finally, the code compiles, but the table containing possible completions does not appear, and I don't have the experience to see why it is not unhidden by shouldChangeCharactersInRange.</p>
  
  <pre><code>class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
  
  @IBOutlet weak var textField: UITextField!
  let autocompleteTableView = UITableView(frame: CGRectMake(0,80,320,120), style: UITableViewStyle.Plain)
  
  var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"]
  var autocompleteUrls = [String]()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      autocompleteTableView.delegate = self
      autocompleteTableView.dataSource = self
      autocompleteTableView.scrollEnabled = true
      autocompleteTableView.hidden = true
  }
  
  func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -&gt; Bool
  {
      autocompleteTableView.hidden = false
      var substring = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
  
      searchAutocompleteEntriesWithSubstring(substring)
      return true     // not sure about this - could be false
  }
  
  func searchAutocompleteEntriesWithSubstring(substring: String)
  {
      autocompleteUrls.removeAll(keepCapacity: false)
      var indexOfPastUrls = 0
  
      for curString in pastUrls
      {
          let substringRange = curString.rangeOfString(curString)
  
          if (indexOfPastUrls  == 0)
          {
              autocompleteUrls.append(curString)
          }
          indexOfPastUrls = indexOfPastUrls + 1
      }
      autocompleteTableView.reloadData()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return autocompleteUrls.count
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
      let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
      var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier, forIndexPath: indexPath) as UITableViewCell
      let index = indexPath.row as Int
  
      cell.textLabel.text = autocompleteUrls[index]
      return cell
  }
  
  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
      textField.text = selectedCell.textLabel.text        
  }
  }
  </code></pre>