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.
QuestArrayon the 1st line but then the rest of the code usesAnsArray.