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

Patricia Duffy
Patricia Duffy
511 Points

Challenge Task 2 ofModifying Array - Why is this wrong: todo.removeAtIndex(2) let item = todo.removeAtIndex(2)

What is the correct answer?

arrays.swift
var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append ("Debug App")
todo.append ("Fix Bugs")

todo.removeAtIndex(2)
let item = todo.removeAtIndex(2)
Matthew Panton
Matthew Panton
15,228 Points

Is there any reason you are removing two items from the array? try removing the first 'todo.removeAtIndex(2)'

2 Answers

Patricia Duffy
Patricia Duffy
511 Points

Thanks for responding. I finally got through the challenge by switching the two lines: 4 let item = todo.removeAtIndex(2) 5 todo.removeAtIndex(2) I don't know why I had to reverse the order. Could you clarify your response. I thought that when I wrote todo.removeAtIndex(2), I was removing item in position 3, and then assigning it a constant. Does assigning it a constant also automatically remove it to? (ie that's why I should delete "todo.removeAtIndex(2)

Matthew Panton
Matthew Panton
15,228 Points

Yes, when you write "let item = todo.removeAtIndex(2)", this removes the item from the array and assigns it to a constant all in one. To just get the third item without removing it you would write "let item = todo[2]" Hope this helps :)

Patricia Duffy
Patricia Duffy
511 Points

Thanks so much...this does help a lot!!!