Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Collections and Control Flow Introduction to Collections Working With Arrays

Yun Yin Ku
Yun Yin Ku
1,493 Points

wrong number to retrieve

it says that I retrieved the wrong number, but I counted the index numbers from 0 which is 1, so the correct number should be 6, right? I don't get it

array.swift
// Enter your code below

//adding arrays to the "list"
var arrayOfInts = [1,2,3,4,5,6]

//append method below
arrayOfInts.append(7+8)

//concatonating method
arrayOfInts + [9]

//reading values from array
let value = arrayOfInts[6]

1 Answer

andren
andren
28,558 Points

Since indexes start at 0 they end up being 1 less than the item number, not 1 more. The first item has an index of 0, the second an index of 1, the third an index of 2, the fourth an index of 3, and the fifth an index of 4.

So to retrieve the fifth item you need to use index 4.

Also there are some issues with your previous lines of code which the challenge checker failed to notice, that I feel I should point out:

arrayOfInts.append(7+8)

That line is likely not doing what you think it does. It will not add 7 and 8 to the array, but will instead perform addition on 7 and 8 and then add the number they equal to arrayOfInts. So the line actually adds the number 15 to the array.

arrayOfInts + [9]

When you concatenate a list Swift generates a new list, it does not modify the existing variable. In order to actually change the variable you need to store the newly generates list back in the arrayOfInts variable, like this:

arrayOfInts = arrayOfInts + [9]

Or use the += shorthand operator which performs the same action, like this:

arrayOfInts += [9]

If you don't do either of those things then the newly generated list is just thrown away, as you have not actually told Swift to do anything with it.