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 What is a Dictionary?

Hi not sure why this answer to your 'try out' for arrays does not compile. It seems to in the playground...

var todo = ["Learn Swift", "Build App", "Deploy App"] // line provided by question, supposed to add strings as below

todo.insert("Debug App", atIndex: 1)

todo.insert("Fix Bugs", atIndex: 2)

5 Answers

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

Hi, mark lesbirel , there's a simpler way to do that actually

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

Thanks William, still not sure why mine did not work though ;-)

William Li
William Li
Courses Plus Student 26,868 Points

because you got the index wrong. the original Array has 3 items, at index 0,1,2, if you were to insert at the end of Array, you need to do

todo.insert("Debug App", atIndex: 3)
todo.insert("Fix Bugs", atIndex: 4)

This will work. That's why += is better at appending sth to the end of Array, that way you don't need to manually count the index yourself.

Thank you!!

Well, If you are doing the one i think you are, then it is best just to do:

todo.insert("Debug App", atIndex: 0)

todo.insert("Fix Bugs", atIndex: 2)

The reason to do that rather than the += [] is because you wont learn that until later. I think so anyway :P

William Li
William Li
Courses Plus Student 26,868 Points

Hi, Gabriel, yeah, perhaps I am getting a little ahead by teaching +=, but your solution won't get pass the grader, as it's asking to append these two items to the end of the Array.

Thanks Gabriel, that was closer to what I did first but must have made a syntax error. Got it, and moved on now...

Why not use append, todo.append("Debug App") etc.? Since you don't have to place the new items in any specific order you can add them to the end of the array. Then you don't have to worry about index numbers. I'm new to Swift coding so I can be way off here. :)