我尝试为文本字段开发自定义自动完成功能。该代码有效,但是如果我删除所有输入,调试器将在写入的行中显示:“致命错误:无法索引空缓冲区”:if createdArrayInDependencyOfInput[indexOfArray] == textFieldText && indexOfArray >= 0 && currentCountOfInput >= 1
这是整个代码:

@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()
}

知道有人解决这个问题吗?
感谢您的关注!

最佳答案

该错误意味着createdArrayInDependencyOfInput为空。您正在清除该行,然后再显示错误:

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

10-08 02:43