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

We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign

I am trying this

let read: [Int] = arrayOfInts[5] and it does not work

array.swift
// Enter your code below
var arrayOfInts: [Int] = [2,3,24,5,4,12]
arrayOfInts.append(100)
arrayOfInts + [2]


arrayOfInts[5]
let read: [Int] = arrayOfints

2 Answers

andren
andren
28,558 Points

There are a number of issues with your code:

  1. The constant has to be named value not read, these challenges are very picky about the names of constants having to be exactly what they instruct them to be.

  2. There is a typo in the name of the array variable it is supposed to be arrayOfInts not arrayOfints (you forgot to capitalize the I)

  3. The type of the new constant should be Int not [Int], since you are retrieving a single Int from the arrayOfInts array and assigning it to it.

  4. Possible the most important issue, as the instructions remind you array indexes start at 0, this means that to access the first item of an array you would use [0], to access the second item you would use [1] and so on. Because of this you are actually accessing the sixth item by using [5]. To access the fifth item you have to use [4].

  5. You have to remove the dangling arrayOfInts[5] that is on its own line not doing anything.

  6. While not technically an error it's worth noting that the line arrayOfInts + [2] is not actually doing anything useful. This operation returns a new array that contains the content of arrayOfInts + 2 but it does not actually modify arrayOfInts. In order to use the returned value you would have to assign it to a variable. If you actually wanted to modify the arrayOfInts variable directly you have to use the += operator instead like this: arrayOfInts += [2]

Edit: Added some points I only noticed after posting.

Yep Thanks for the answer, i did not read correctly the instructions :/