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

Brook Wolcott
Brook Wolcott
874 Points

How do I remove an item from an array?

I am working on a challenge asking me to use the removeAtIndex() method to remove the 6th item (index 5) from an array. Since removeAtIndex() has been replaced in Swift 3, I cannot test my code in XCode. However, I have reviewed it about 50 times now and I can't find a problem.

Can anyone tell me when I keep getting a "Bummer" message saying to make sure I remove the 6th item from Index 5?

arrays.swift
// Enter your code below

var arrayOfInts = [12, 13, 24, 55, 44, 7]

arrayOfInts.append(77)
arrayOfInts = arrayOfInts + [23]

let value = arrayOfInts[4]

arrayOfInts.removeAtIndex(5)

let discardedValue = (arrayOfInts)

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hey Brook, the wording is a bit tricky.

It's saying to remove the 6th value and place that value (the 6th you have removed) into the 'discardedValue'. The return value for '.removeAtIndex()' is the item removed.

let discardedValue = arrayOfInts.removeAtIndex(5)
Brook Wolcott
Brook Wolcott
874 Points

Ahh... that makes more sense. Thanks, Damien!