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

Richard Jones
Richard Jones
3,500 Points

Working with Arrays: code challenge

I am having difficulty with the third part of the code challenge. We were taught how to remove values from arrays but it was never discussed in the video how to add that value to a constant. Amazing how many times the code challenges ask me to do something not explained. You guys must be having quite a laugh.

So the only way I can move on with my lesson is to post this question, find my question in the community and follow the link to the discussion about my lesson and see if someone can explain what to do. How nice would it be to add a link on the code challenge page directly to the discussion about that lesson. I guess it is better to stump your students.

arrays.swift
// Enter your code below
var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(12)
arrayOfInts = arrayOfInts + [13]
let value = arrayOfInts[4]

1 Answer

Moritz Lang
Moritz Lang
25,909 Points

Hi Richard,

removeAtIndex(0)

returns the value you're removing.

The correct answer would be as easy as this:

var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts += [8]
let value = arrayOfInts[4]
let discardedValue = arrayOfInts.removeAtIndex(5)
Alex Key
Alex Key
463 Points

Updated for swift 3: you'll need to use the new syntax for .removeAtIndex(5) which is now:

remove(at: 5)