How can I fix “fatal error: can't index empty buffer”

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: if createdArrayInDependencyOfInput[indexOfArray] == textFieldText && indexOfArray >= 0 && currentCountOfInput >= 1

Here is the whole code:

  • How do I get the error message in Swift 2.0?
  • Fix for Xcode's indiscernible highlighting of inline errors?
  • Why does “try catch” in Objective-C cause memory leak?
  • What's Dead & Exploded in Swift's exception stack?
  • Error “Call can throw, but is not marked with 'try' and the error is not handled”
  • Error-Handling in Swift-Language
  • @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) -> [String] {
        var arrayOfCharacters: [String] = []
        if indexNumber <= 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() -> Bool {
            if currentCountOfInput < formerCountOfInput {
                return true
            }
            else {
                return false
            }
        }
    
        func concatenateArrayOfCharactersToString(array: [String]) -> String {
            var arrayForDelegate: [String] = [""]
            var positionOfRun: Int = 0
            if arrayForDelegate[0] == "" {
                arrayForDelegate.removeLast()
            }
            while positionOfRun <= array.endIndex - 1{
                arrayForDelegate.append(String(array[positionOfRun]))
                positionOfRun += 1
            }
    
            while (arrayForDelegate.endIndex - 1) >= 1 {
                arrayForDelegate[0] = arrayForDelegate[0] + arrayForDelegate[1]
                arrayForDelegate.removeAtIndex(1)
            }
            if currentCountOfInput >= 1 {
                return arrayForDelegate[0]
            }
            else {
                return ""
            }
        }
    
    
        while countOfRun < arrayOfSubjects.count {
            if !backspaceWasPressed() {
                var convertedStringInFormOfArrayOfStrings: [String] = convertStringOfArrayToAnArrayOfCharacters(arrayOfSubjects, withIndex: countOfRun)
                if currentCountOfInput == 1 {
                    createdArrayInDependencyOfInput.append(convertedStringInFormOfArrayOfStrings[currentCountOfInput-1])
                }
                else if countOfRun >= 0 {
                    if countElements(arrayOfSubjects[countOfRun]) != countElements(createdArrayInDependencyOfInput[countOfRun]){
                    createdArrayInDependencyOfInput[countOfRun] = createdArrayInDependencyOfInput[countOfRun] + convertedStringInFormOfArrayOfStrings[currentCountOfInput-1]
                    }
                }
                countOfRun += 1
            }
            else if backspaceWasPressed() && createdArrayInDependencyOfInput != [] {
                var convertedString: [String] = convertStringOfArrayToAnArrayOfCharacters(createdArrayInDependencyOfInput, withIndex: countOfRun)
                if countElements(arrayOfSubjects[countOfRun]) > currentCountOfInput  && currentCountOfInput >= 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 && indexOfArray >= 0 && currentCountOfInput >= 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()
    }
    

    Know anybody the solve for this problem?
    Thank You for Your attention!

    Solutions Collect From Internet About “How can I fix “fatal error: can't index empty buffer””

    That error means createdArrayInDependencyOfInput is empty. You’re clearing it just before the line that gives you the error:

    if textFieldText.isEmpty {
            createdArrayInDependencyOfInput.removeAll(keepCapacity: false)
        }