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

Finally, we forgot to add that we need to learn iOS development prior to building an app

I don't understand why is not working. Can somebody please show some light. :(

Here is my code...

var todo = ["Learn Swift", "Build App", "Deploy App"] todo.append("Debug App") todo.append("Fix Bugs") todo.removeAtIndex (1) let item = todo.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)

Your code is removing both Build App and Deploy App. What is your purpose with this? Do you want to keep these in and just add in Learn iOS? If so just remove these lines: todo.removeAtIndex (1) let item = todo.removeAtIndex (1)

The above code (not in the code field but just in text) works fine for me and outputs: [Learn Swift, Learn iOS, Debug App, Fix Bugs]

Removing the lines I mentioned above provides: [Learn Swift, Learn iOS, Build App, Deploy App, Debug App, Fix Bugs]

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hello, Erik Figueroa .

The problem you have in your code is these two lines you wrote at part 2 of the challenge

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

Please remove the third item ("Deploy App") and assign it to a constant named item.

The challenge is asking you to remove ONLY the third item from the list and assign it to constant item; but with the above 2 lines of code, you removed both the 2nd and 3rd item from the list, and assigned 3rd item to constant item.

So, even though part 2 challenge's grader let you pass, because it checks that your constant item is having the correct value; however, when you get to part 3 of the challenge, though your code todo.insert("Learn iOS", atIndex: 1) is 100% the correct solution, there's no way you can pass it because you've permanently removed the 2nd item from todo in the previous challenge which the grader is expecting todo to have.

The correct way to go about solving part 2 challenge is do it in just one line

let item = todo.removeAtIndex(2)

And the rest of your code is correct.

Thank you.

The question is this:

Finally, we forgot to add that we need to learn iOS development prior to building an app. Insert a new item titled "Learn iOS" at the second position in the todo array.

My Code is this:

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: 0)

Error is this:

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

I'm giving it the right code ( I think) and still not working. I need to add "Learn iOS" to index 1 without removing anything. Otherwise I get the error.

Typo: Last code should be...

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

Still not woring...

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

Can you leave the code as simply as this? It works under playgrounds. It outputs: [Learn Swift, Learn iOS, Build App, Deploy App, Debug App, Fix Bugs]

Might be due to the +=

Man, this one exercise really got me. It works just fine on Playgrounds. Thanks for the replies though.