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 trialDaniel Smith
674 PointsInsert a new item titled "Learn iOS" at the second position in the todo array. No example given to complete this, help
How do I insert into the array? No example given
var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append("Debug App")
todo.append("Fix Bugs")
let item = todo.removeAtIndex(2)
2 Answers
Stone Preston
42,016 PointsAmit goes over the insert method at 7:32 of this video. you may want to go back and review that part.
here is an example of how to use the insert method
myArray.insert("This String", atIndex: 0)
it takes two arguments, the value you want to insert, and the index where you want to insert it.
in your case, you want to insert "Learn iOS" at the second position in the string, which is index 1:
todo.insert("Learn iOS", atIndex: 1)
Tom Ridley
3,673 PointsYou need to specify the index where you want to add the item. The following is taken from the swift documentation
var array = [1, 2, 3]
array.insert(0, atIndex: 0)
// array is [0, 1, 2, 3]
In you example to insert "Learn iOS" at the position 2 you write the following:
todo.insert("Learn iOS", atIndex: 1)
remember that the second item is at index 1 because arrays count from 0.
Daniel Smith
674 PointsThank you!
Daniel Smith
674 PointsDaniel Smith
674 PointsThank you!