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

Having Trouble with question/code. Not sure what is wrong.

Not sure If there is something wrong with my code. I can't seem to figure out the answer.

??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.

code: todo = ["Learn Swift", "Build App", "Deploy App"] todo += ["Debug App","Fix Bugs"]

let item = todo.removeAtIndex(4) item

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

4 Answers

Stone Preston
Stone Preston
42,016 Points

you are not indexing the array correctly. Remember that array indexes start at 0. So the first element is index 0, the second is index 1, etc etc. also remove the single todo and item you have out beside a few of your lines. thats not going to compile.

the challenge should not have let you pass task 2. you removed the wrong index. you are supposed to remove the third element, which is index 2. then for task 3 insert an item at the second position in the array, which is index 1.

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

Yes I think I was thrown off because my removeAtIndex worked. Thank you for the help man!

Thanks Stone Preston, I totally forgot about +=

Challenge task one & two: Todo.append (“Debug App”) Todo.append (“Fix Bugs”)

Let item = todo.removeAtIndex (2) Todo.insert(“Fix Bugs”, at index: 2)

(these are the answers I gave)

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

and it would let me pass.

It's weird, sometimes solutions that compile perfectly fine inside of playgrounds won't compile in the treehouse compiler.

So my code was as follows and won't compile....what gives? Why does that happen?

var todo = ["Learn Swift", "Build App", "Deploy App"] 
todo.append("Debug App") // using an acceptable solution from the previous quiz here
todo.append("Fix bugs")  // and here
let item = todo.removeAtIndex(2) 
todo.insert("Learn iOS", atIndex: 1)