1

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.

5
  • 1
    Is this a typo in your question? You have QuestArray on the 1st line but then the rest of the code uses AnsArray. Commented Jul 18, 2017 at 6:56
  • 1
    Considering your note I would like to mention that you can not have empty blocks in an array. You have to put something at index 0 & 1 if you intend to put a particular value/object at index 2. It might be a simple object but it must have previous indices filled Commented Jul 18, 2017 at 6:57
  • @rmaddy I have edited thanx for correcting Commented Jul 18, 2017 at 6:57
  • Your main error is related to cell count on list view (tableview or collection view) your are using , Please post whole code of controller . Commented Jul 18, 2017 at 7:00
  • What do you expect to happen if the array is empty and indexPath.row is 17? Commented Jul 18, 2017 at 7:04

2 Answers 2

2

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.

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.

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"
}
Sign up to request clarification or add additional context in comments.

Comments

2

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.