0

I have a method (contents below) where queue2 is just an [Int]. I printed a lot of things to see if everything was working up to a point.

public func cool(item: Int) {
    println(item)
    println(back)
    //queue2.insert(item, atIndex: back)
    queue2[back] = item
    println(queue2.description)
    println("done")
}

The problem is this fails at runtime and I don't know why. Apple docs say you can set the value of any index in an array with this notation, but it doesn't work. If I uncomment the commented line and comment out the one below it, everything runs fine but it doesn't provide the functionality I need. What gives?

2
  • It stops execution with a exc_bad_instruction (code=exc_i386_invop subcode=0x0) Commented Jul 21, 2015 at 1:40
  • back = 0, and the queue is initiated to an array with capacity 20. Commented Jul 21, 2015 at 1:42

1 Answer 1

2

If queue2 is empty, this line is illegal no matter what back is:

queue2[back] = item

You cannot refer to an index that doesn't exist, and an empty array has no indexes (indices).

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

2 Comments

As for what happens when you use queue2.insert(item, atIndex: back), the problem here is that you say "it doesn't provide the functionality I need", but you have not explained what that functionality is. That code is doing exactly what you are asking it to do; neither it, nor we, have any way of knowing what else you might have had in mind...
how can i do that if i want? as its not posssible but i want to do that? can i realy do that?

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.