0

I am trying to write some code which will append my array to this: ["e", "0", " ", "0"] but instead i am getting this : ["e", "0", "0"]. I don't understand why it is displaying the latter, since as far as I understand, index becomes 3 when i add 2 to it. Additionally I am also getting "fatal error: Array index out of range" when i try running this : TheTape.insert("0", atIndex: index+2). Here I also don't understand why it is out of range when i didnt specify the size of the array.

Thanks for your help!

import UIKit
var TheTape = [Any]()
var index = 0 

    if(TheTape.isEmpty){

        TheTape.insert("e", atIndex: 0)
        print(TheTape)
        index++
        TheTape.insert("0", atIndex: index)
        print(TheTape)
        index + 2
        TheTape.insert("0", atIndex: index)
        print(TheTape)      
    }
3
  • insert stuffs new stuff into the array at the specified position. index + 2 doesn't exist, so you're out of range. Commented Feb 9, 2016 at 19:05
  • insert what stuffs? I am not understanding what you are saying Commented Feb 9, 2016 at 19:10
  • Please note that the syntax index++ will be deprecated in Swift 2.2 and illegal in Swift 3.0. So the time to give up using it is now. Commented Feb 9, 2016 at 19:26

2 Answers 2

2

Basically you're doing this;

TheTape.insert("e", atIndex: 0)       // array = ['e']           (insert at 0)
print(TheTape)
index++                               // index 0 -> 1
TheTape.insert("0", atIndex: index)   // array = ['e', '0']      (insert at 1)
print(TheTape)
index + 2                             // Does nothing
TheTape.insert("0", atIndex: index)   // array = ['e', '0', '0'] (insert at 1)
print(TheTape)      

As you can see, index + 2 does nothing (or rather, it calculates index + 2 but does not store the result anywhere), if you want to increase index by 2, use index += 2.

Inserting at an index outside the array is an error and will give the error you're seeing. If you want an empty value separating the values, you'll need to insert that first.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply! Why doesn't index+2 change the value of index? I am adding 1+2 will this not become 3? Also i don't understand why i can't add a position if i have specified an array with no size? Apologies if this is a stupid question, i am very new to programming
@user3395936 index + 2 calculates the value, but does not store it anywhere. index = index + 2 would work, or index += 2 which (in most aspects) is a shortcut for the same thing.
2

First, index + 2 doesn't change value of index

Second, you can't do TheTape.insert("0", atIndex: index+2) because your array TheTape has only size of 2, at the time, you are inserting an object at position 4 which requires at least at array with 3 elements.

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.