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

Rayhan Miah
Rayhan Miah
2,369 Points

Whats wrong with my code

Whats wrong with my code Im removing does anyone have the code to this or know whats wrong

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

3 Answers

joseph leporati
seal-mask
.a{fill-rule:evenodd;}techdegree
joseph leporati
iOS Development Techdegree Student 3,304 Points

Rather than showing you exactly how I did this - let's figure out why your code isn't working first. You did perfect until we got to the 6th line of your code.

arrayOfInts.removeAtIndex(7)
let discardedValue = arrayOfInts

There is nothing wrong with the first line from this snippet. This will remove the item at index 7. The next line though you are setting the entire array of numbers to the value discardedValue - you're also selecting the wrong index. What you should have is this:

let discardedValue = arrayOfInts.removeAtIndex(5)

The directions tell you to store the item removed at the index you specify into a constant named 'discardedValue'. You failed to do this - which is why it failed.

Let me know if you have any questions.

Alexander Smith
Alexander Smith
10,476 Points

I did it like this

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

Another way you could potentially do this, is to add two arrays together. More code heavy, but its another way of completing the task if you were interested.

// Enter your code below
var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(7)
var newInt = [8]
var newArrayOfInts = arrayOfInts + newInt
let value = newArrayOfInts[4]
let discardedValue = newArrayOfInts.removeAtIndex(5)

Cheers, Marc