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 Basics (retired) Collections Modifying an Array

getting error " Bummer! Your 'todo' variable has the wrong value in it.

Bummer! Your 'todo' variable has the wrong value in it. .

var todo = ["Learn Swift", "Build App", "Deploy App"]

todo += ["Debug App", "Fix Bugs"]

todo.removeAtIndex(1)

let item = todo1.removeAtIndex(1)

todo.insert("Learn iOS", atIndex: 1)

arrays.swift
var todo = ["Learn Swift", "Build App", "Deploy App"]

todo += ["Debug App", "Fix Bugs"]

todo.removeAtIndex(1)
let item = todo.removeAtIndex(1)

todo.insert("Learn iOS",  atIndex: 1)

2 Answers

Christopher Augg
Christopher Augg
21,223 Points

Mathew,

Very close! Just a couple of small bugs when you do the following:

todo.removeAtIndex(1)
let item = todo.removeAtIndex(1)

When you call removeAtIndex(1) the first time, the second element ("Build App") is removed instead of ("Deploy App"). Furthermore, it is not assigned to anything. This makes the array go from 5 elements to 4 and look like this:

["Learn Swift", "Deploy App", "Debug App", "Fix Bugs"]

You then proceed to remove another element at index 1 and place it into the constant item. You get ("Deploy App") this time but your array looks like:

["Learn Swift", "Debug App", "Fix Bugs"]

You then use insert to get a final array of:

["Learn Swift", "Learn iOS", "Debug App", "Fix Bugs"]

When it should look like:

["Learn Swift", "Learn iOS", "Build App", "Debug App", "Fix Bugs"]

Therefore, you will just want to delete the todo.removeAtIndex(1) line and change the let item = todo.removeAtIndex(1) to let item = todo.removeAtIndex(2). Remember, arrays start at 0. So, when we say to remove at index 2, we are counting 0..1..2 to get the third element in the array.

The final code:

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo += ["Debug App", "Fix Bugs"]
let item = todo.removeAtIndex(2)
todo.insert("Learn iOS", atIndex: 1)

Hope this helps.

Regards,

Chris

Hi Mathew ,

I copy pasted your code and for step 1 it worked fine but there are minor errors for 2nd and 3rd step. For "STEP 2" there are 2 errors ----------

[ERROR 1] -> it asks to remove 3rd item i.e. "Index = 2" and you wrote "index = 1"

[ERROR 2] -> (Here i am taking the correct index i.e. 2 ) When you typed

" todo.removeAtIndex(2) " for first time : Result was <<< var todo = ["Learn Swift", "Build App" ,"Debug App", "Fix Bugs"] >>> i.e. (3rd word was removed and now the 4th word became the 3rd word) and again when you typed

" let item = todo.removeAtIndex(2) " : Result was <<< ["Learn Swift", "Build App", "Fix Bugs"] >>>
i.e. 3rd word got removes but this time 3rd word was "Debug App".

Now if you see you removed 2 words from the array. So code should be run once i.e. let item = todo.removeAtIndex(2) --- Here at one "call" index is returned and assigned to the variable.

NOTE- Actually your approach works well while initializing any "return variables"etc but not on using default returning values of array.