gpt4 book ai didi

error-handling - 如何修复 "fatal error: can' t 索引空缓冲区”

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:36 24 4
gpt4 key购买 nike

我尝试为文本字段开发自定义自动完成功能。代码有效,但如果我删除所有输入,调试器会在写入的行中显示:“ 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()
}

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

最佳答案

该错误意味着 createdArrayInDependencyOfInput 为空。你在给你错误的行之前清除它:

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

关于error-handling - 如何修复 "fatal error: can' t 索引空缓冲区”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com