我如何解决"致命错误:无法索引空缓冲区"

时间:2014-10-02 07:28:13

标签: error-handling swift ios8 xcode6 fatal-error

我尝试为文本字段开发自定义自动完成功能。代码可以工作,但如果我删除了所有输入,调试器会说:“致命错误:无法索引空缓冲区”在写入的行中: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()
}

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

1 个答案:

答案 0 :(得分:3)

该错误表示createdArrayInDependencyOfInput为空。您只是在提供错误的行之前清除它:

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