title: How can I fix "fatal error: can't index empty buffer"
body: <p>I try to develop a custom autocompletion for a textfield. The code works but if I delete all my input, the debugger says: "fatal error: can't index empty buffer" in the line where is written: <code>if createdArrayInDependencyOfInput[indexOfArray] == textFieldText &amp;&amp; indexOfArray &gt;= 0 &amp;&amp; currentCountOfInput &gt;= 1</code></p>
  
  <p>Here is the whole code:</p>
  
  <pre><code>@IBOutlet weak var MyTextField: UITextField!
  @IBOutlet weak var MyPlaceholderTextField: UITextField!
  
  var arrayOfSubjects: [String] = ["Informatik", "Italienisch", "Lateinisch", "Biologie", "Mathematik"]
  var createdArrayInDependencyOfInput: [String] = []
  var formerCountOfInput: Int = 0
  var foundSubject: Bool = false
  var indexOfArray: Int = 0
  
  func printValue(value: Int) {
      println("\(value)")
  }
  
  func convertStringOfArrayToAnArrayOfCharacters(array: [String], withIndex indexNumber: Int) -&gt; [String] {
      var arrayOfCharacters: [String] = []
      if indexNumber &lt;= array.count - 1 {
          for character in array[indexNumber] {
              var string = String(character)
              arrayOfCharacters.append(string)
          }
      }
      return arrayOfCharacters
  }
  
  @IBAction func MyTextFieldEditingChanged(sender: AnyObject) {
      var currentCountOfInput: Int = countElements("\(MyTextField.text)")
      var countOfRun: Int = 0
  
  
      func backspaceWasPressed() -&gt; Bool {
          if currentCountOfInput &lt; formerCountOfInput {
              return true
          }
          else {
              return false
          }
      }
  
      func concatenateArrayOfCharactersToString(array: [String]) -&gt; String {
          var arrayForDelegate: [String] = [""]
          var positionOfRun: Int = 0
          if arrayForDelegate[0] == "" {
              arrayForDelegate.removeLast()
          }
          while positionOfRun &lt;= array.endIndex - 1{
              arrayForDelegate.append(String(array[positionOfRun]))
              positionOfRun += 1
          }
  
          while (arrayForDelegate.endIndex - 1) &gt;= 1 {
              arrayForDelegate[0] = arrayForDelegate[0] + arrayForDelegate[1]
              arrayForDelegate.removeAtIndex(1)
          }
          if currentCountOfInput &gt;= 1 {
              return arrayForDelegate[0]
          }
          else {
              return ""
          }
      }
  
  
      while countOfRun &lt; arrayOfSubjects.count {
          if !backspaceWasPressed() {
              var convertedStringInFormOfArrayOfStrings: [String] = convertStringOfArrayToAnArrayOfCharacters(arrayOfSubjects, withIndex: countOfRun)
              if currentCountOfInput == 1 {
                  createdArrayInDependencyOfInput.append(convertedStringInFormOfArrayOfStrings[currentCountOfInput-1])
              }
              else if countOfRun &gt;= 0 {
                  if countElements(arrayOfSubjects[countOfRun]) != countElements(createdArrayInDependencyOfInput[countOfRun]){
                  createdArrayInDependencyOfInput[countOfRun] = createdArrayInDependencyOfInput[countOfRun] + convertedStringInFormOfArrayOfStrings[currentCountOfInput-1]
                  }
              }
              countOfRun += 1
          }
          else if backspaceWasPressed() &amp;&amp; createdArrayInDependencyOfInput != [] {
              var convertedString: [String] = convertStringOfArrayToAnArrayOfCharacters(createdArrayInDependencyOfInput, withIndex: countOfRun)
              if countElements(arrayOfSubjects[countOfRun]) &gt; currentCountOfInput  &amp;&amp; currentCountOfInput &gt;= 0 {
                  convertedString.removeLast()
              }
              createdArrayInDependencyOfInput[countOfRun] = concatenateArrayOfCharactersToString(convertedString)
              countOfRun += 1
          }
  
      }
      if countElements(createdArrayInDependencyOfInput[0]) == 0 {
          createdArrayInDependencyOfInput.removeAll(keepCapacity: false)
      }
  
      while !foundSubject {
          var textFieldText: String = MyTextField.text
  
          if textFieldText.isEmpty {
              createdArrayInDependencyOfInput.removeAll(keepCapacity: false)
          }
          if createdArrayInDependencyOfInput[indexOfArray] == textFieldText &amp;&amp; indexOfArray &gt;= 0 &amp;&amp; currentCountOfInput &gt;= 1 {
              MyPlaceholderTextField.text = arrayOfSubjects[indexOfArray]
              foundSubject = true
              break
          }
          else {
              MyPlaceholderTextField.text = ""
              foundSubject = false
              indexOfArray += 1
          }
      }
      foundSubject = false
      formerCountOfInput = currentCountOfInput
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
      MyTextField.becomeFirstResponder()
      MyPlaceholderTextField.textColor = UIColor.lightGrayColor()
  }
  </code></pre>
  
  <p>Know anybody the solve for this problem?
  Thank You for Your attention!</p>