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 trialmark lesbirel
2,012 PointsHi 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
Courses Plus Student 26,868 PointsHi, mark lesbirel , there's a simpler way to do that actually
todo += ["Debug App", "Fix Bugs"]
mark lesbirel
2,012 PointsThank you!!
Gabriel Mora
2,378 PointsWell, 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
Courses Plus Student 26,868 PointsHi, 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.
mark lesbirel
2,012 PointsThanks Gabriel, that was closer to what I did first but must have made a syntax error. Got it, and moved on now...
ktd
2,413 PointsWhy 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. :)
mark lesbirel
2,012 Pointsmark lesbirel
2,012 PointsThanks William, still not sure why mine did not work though ;-)
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointsbecause 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
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.