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

Juan Galindo
PLUS
Juan Galindo
Courses Plus Student 404 Points

arrayOfInts.remove(at: 4) marks an error, haven't been able to complete this challenge!

3

array.swift
// Enter your code below
var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(7)
arrayOfInts += [8]

arrayOfInts.remove(at: 4)
let value = arrayOfInts
russ whelchel
russ whelchel
10,406 Points

Looks like you concatenated the third and fourth step of this challenge. The third step was to assign the 5th item from the array to a constant named 'value':

let value = arrayOfInts[4]

and the fourth step was to remove the 6th item from the array and assign it to a constant named discardedValue (or something like that):

let discardedValue = arrayOfInts.remove(at: 5)

1 Answer

Everton Carneiro
Everton Carneiro
15,994 Points

In task 3, they asked to: "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." But you are assigning the entire array when you write:

let value = arrayOfInts

In task 4: "For the last task, remove the 6th item from the array and assign the result to a constant named discardedValue.

To remove an item, use the method remove(at:) and put the index number in between parentheses that you want to remove." You have to remember that the index starts at 0. Also you forget to assign the value to a constant named discardedValue in your code:

arrayOfInts.remove(at: 4)

Also I don't know why, but your answers came in a different order. Especially working with collections, you need to be careful about the order of your line of code since we are appending and removing stuffs. I hope this helps.