2

I keep getting this error in my func

I'm trying to read the value in array answerRecord. I uses a global var arrayCount, which keep track which index im currently pointing to.

func buttonColourControl(){
    switch answerRecord[arrayCount]{
    case1: xxxxxxx

I did a println in my earlier func and it return a value of int 1 for the var arrayCount Therefore arrayCount is not empty. So it should be able to interpret the array as:

*assuming arrayCount is now 1 answerRecord[arrayCount] should be interpreted as answerRecord[1] Please correct me if im wrong

@IBAction func nextButtonClicked(sender: UIButton) {
    arrayCount = ++arrayCount
    question.text = spouseQuesion[arrayCount]
    controlBackNextButton()
    answer1Label.text = spouseAnswer1[arrayCount]
    answer2Label.text = spouseAnswer2[arrayCount]
    answer3Label.text = spouseAnswer3[arrayCount]
    println(arrayCount)
    buttonColourControl()
}
9
  • Which line is the error occurring on? Commented Dec 19, 2014 at 5:25
  • at the switch statement inside the func buttonColourControl() Commented Dec 19, 2014 at 5:26
  • So, this error means that when you say answerRecord[arrayCount], the value of arrayCount does not correspond to any index number of answerRecord. You cannot just use any value here. It must be between 0 and the size of the array. The count of the array itself is too big. Commented Dec 19, 2014 at 5:28
  • Where does the value of arrayCount come from? What is actually in answerRecord? Commented Dec 19, 2014 at 5:28
  • Hi Matt, i know where's my bug is. Thanks for your clear explanation. I read the next element before i append a value into it. now i want to do a IF statement to check if the arrayRecord[arrayCount] is empty, but i'm getting another error. if answerRecord[arrayCount] != nil <error: cannot invoke != with an arguement> -> What does this means? Commented Dec 19, 2014 at 6:14

1 Answer 1

8

Let's say you have an array with one object in it:

let arr = ["hello"]

The only valid index into that array is 0. arr[0] is legal. arr[1] is not. The array has 1 element but its index number is 0.

This is true for any array. Every array holds some number of elements. It might be 0 elements, in which case no index is legal. It might be 3 elements, in which case you can refer to the array's elements by index numbers 0, 1, and 2. And so on. That's all. Those are the rules. You cannot use any other index number or you will crash.

So the error message is simply telling you that you are making that mistake. You have an array answerRecord and it has some number of elements - I have no idea how many, and it doesn't matter. Then you are using the expression answerRecord[arrayCount] and the value of arrayCount is outside the bounds I have just explained. That's all you need to know. The error message tells you the bug in your program. Now you can fix it.

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

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.