I have an array of strings and I want to check if the array at index is empty than I have to insert element, if it is not empty, I have to change the value of this element. I tried this:
我有一个字符串数组,我想检查索引处的数组是否为空,而不是插入元素,如果它不为空,我必须更改此元素的值。我试过这个:
var AnsArray = [String]()
let a_id = "Hello"
if (AnsArray[Indexpath.row].isEmpty){
AnsArray.insert(a_id, at: Indexpath.row)
}
else {
AnsArray[Indexpath.row] = a_id
}
But it is giving an error:
但它给出了一个错误:
fatal error: Index out of range
致命错误:索引超出范围
I also tried this:
我也试过这个:
if (AnsArray[Indexpath.row] == "" ) {
AnsArray.insert(a_id, at: Indexpath.row)
}
else {
AnsArray[Indexpath.row] = a_id
}
It is also giving the same error.
它也给出了同样的错误。
Note: I can not use array.count because array can hold elements in specific index for example at index = 0 it can be empty and at index = 2 it can be stored some data.
注意:我不能使用array.count,因为数组可以保存特定索引中的元素,例如在index = 0时它可以为空,而在index = 2时,它可以存储一些数据。
2 个解决方案
#1
1
It seems you want to access item outside the bounds of an array. Your array is empty, so calling AnsArray[whateverIndexPath]
will always result in this error.
看来你想要访问数组边界之外的项目。您的数组为空,因此调用AnsArray [whateverIndexPath]将始终导致此错误。
What's important is that Array
cannot hold nil
values - if your array is empty, you can't just "check at speciffic index", like you described you want to.
重要的是Array不能保存nil值 - 如果你的数组是空的,你不能只是“检查特定的索引”,就像你想要的那样。
Also, you can't insert
an item in empty array - the index you insert at must be already valid before the insertion.
此外,您不能在空数组中插入项 - 您插入的索引必须在插入之前已经有效。
So, if you want to insert something to an array, you need to start at the very beginning, with append
. Or, alternatively, pre-populate your array with empty strings.
所以,如果你想在数组中插入一些东西,你需要从头开始,并附加。或者,或者使用空字符串预填充数组。
AnsArray = [String](repeating: "", count: yourDesiredArrayCount)
if (AnsArray[Indexpath.row].isEmpty){
AnsArray[Indexpath.row] = "newValue"
}
#2
1
You should check first index is available.if you directly access element by index you may get Index out of range exception if it don't have. So you can check by Indices with contains property
你应该检查第一个索引是否可用。如果你直接访问索引元素,你可能会得到索引超出范围异常,如果它没有。因此,您可以通过包含属性的指数进行检查
if AnsArray.indices.contains(YOUR_INDEX) {
//Now you can check whether element is empty or not.
// Do your tuffs
}else {
//insert new element
}
#1
1
It seems you want to access item outside the bounds of an array. Your array is empty, so calling AnsArray[whateverIndexPath]
will always result in this error.
看来你想要访问数组边界之外的项目。您的数组为空,因此调用AnsArray [whateverIndexPath]将始终导致此错误。
What's important is that Array
cannot hold nil
values - if your array is empty, you can't just "check at speciffic index", like you described you want to.
重要的是Array不能保存nil值 - 如果你的数组是空的,你不能只是“检查特定的索引”,就像你想要的那样。
Also, you can't insert
an item in empty array - the index you insert at must be already valid before the insertion.
此外,您不能在空数组中插入项 - 您插入的索引必须在插入之前已经有效。
So, if you want to insert something to an array, you need to start at the very beginning, with append
. Or, alternatively, pre-populate your array with empty strings.
所以,如果你想在数组中插入一些东西,你需要从头开始,并附加。或者,或者使用空字符串预填充数组。
AnsArray = [String](repeating: "", count: yourDesiredArrayCount)
if (AnsArray[Indexpath.row].isEmpty){
AnsArray[Indexpath.row] = "newValue"
}
#2
1
You should check first index is available.if you directly access element by index you may get Index out of range exception if it don't have. So you can check by Indices with contains property
你应该检查第一个索引是否可用。如果你直接访问索引元素,你可能会得到索引超出范围异常,如果它没有。因此,您可以通过包含属性的指数进行检查
if AnsArray.indices.contains(YOUR_INDEX) {
//Now you can check whether element is empty or not.
// Do your tuffs
}else {
//insert new element
}