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 2.0 Collections and Control Flow Introduction to Collections Working with Arrays

Challenge Question Confusion

In this challenge it asks to do "We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign the result to a constant named value."

First, what does it mean to Retrieve? I'm having confusion about that also This is my code but an error shows up.

Please help

arrays.swift
// Enter your code below

var arrayOfInts = [1,2,3,4,5,6]

arrayOfInts.append(7)

arrayOfInts += [8]

let value = [5]

3 Answers

For your first question, see below.

Value: 1, 2, 3, 4, 5, 6 Index: 0, 1, 2, 3, 4, 5 As you can see, the 5th value (which is 5) is the 4th index of the array. Hope that clarifies. If you're still confused, experiment in Xcode with array indexes and values.

For your second question, the reason why you might have an error is because you're trying to remove the arrayOfInts' seventh index, which doesn't exist. If you look at my model above, you should have 5 indexes. If you want to remove the value of 6 from arrayOfInts, try this.

arrayOfInts.removeAtIndex(5)

Since the 5th index has a value of 6, you will erase the value of 6 from the array. Hope this all helps, and please contact me if you have more questions!

thank you! Really appreciate it!

Retrieve means extract the value from the array and assign it as a value to a new constant or variable. The reason why your code doesn't work is because when you declare a new constant or variable and you want to assign a value from a previous array, you need to specify the array name. Your last line should look like this.

let value = arrayOfInts[4]

Also remember, array indexes start at 0, so when you count to 5, 4 is your value since 0 is the first value. Any other questions, let me know!

Hi, thanks for the help. But, when I count to 5 isn't 6 the value how is it 4?

And also, The other question is "To remove an item, use the method removeAtIndex() and put the index number in between parentheses that you want to remove."

This is the code that I did, but it is showing an error:

// Enter your code below

var arrayOfInts = [1,2,3,4,5,6]

arrayOfInts.append(7)

arrayOfInts += [7]

let value = arrayOfInts[4]

arrayOfInts.removeAtIndex(7)

let discardValue = arrayOfInts

Please help. Thanks!

Happy to help! :)