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

Charles Williams
Charles Williams
371 Points

Trying to Insert "Learn iOS" into 2nd position of array todo. Error 'todo' variable has the wrong value.

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)

Danny Yassine
Danny Yassine
9,136 Points

Is it this your trying to accomplish?

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)

I tried it playground and it worked. No Errors. This is what I got in the console when printing the each statement above:

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

Missing anything to your question?

Charles Williams
Charles Williams
371 Points

This worked fine in Playgrounds, but it didn't pass the challenge, so I'm not sure what's wrong.

2 Answers

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

Hi Charles Williams

At first glance, there's issue with codes at line 3 & 4, as the part 2 of the challenge only asks for removing the 3rd from the Array and assign it to a constant, but line 3 of your code removed the 2nd item in the Array as well, which isn't what the challenge is looking for, and therefore fail to pass the grader check. (yeah, the grader do checks for whether todo has the correct length and items every time)

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

Make sense?

Charles Williams
Charles Williams
371 Points

On part 2 of the assignment, I originally removed the 3rd from the Array, however, it was flagged as incorrect. I changed the value to remove the 2nd and it worked. That's why it is set to 1 instead of 2.

William Li
William Li
Courses Plus Student 26,868 Points

without seeing your original code, it's hard to pinpoint what went wrong. However, I should mention that you must write it in 1 line, remove item from Array and assign to a constant in one line let item = todo.removeAtIndex(2) like such.

If you write it in two line, similar to the code you posted.

todo.removeAtIndex(2)          // remove 2nd item from todo array
let item = todo.removeAtIndex(2)   // remove 2nd item from todo array AGAIN!

This is wrong, because it removes 2 elements from the Array. Hope it helps.

Michael Reining
Michael Reining
10,101 Points

I think the error is in a different line. Check the removeAtIndex. The value should be 2 not 1

This here worked for me:

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

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app.

Code! Learn how to program with Swift