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

with the todo.insert I'm not sure what I'm doing wrong

need help with todo.insert array

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

3 Answers

Just need to modify your atIndex: 2 to show atIndex: 1.

Remembering that indexes start at 0

Grant Williamson
Grant Williamson
1,622 Points

You're adding the "Learn iOS" string at index 2. Remembering that arrays start from 0, it will look something like this:

0 => Learn Swift
1 => Build App
2 => Learn iOS // Added with insert function. What was here will be moved down one place.
3 => Deploy App
4 => Debug App
5 => Fix Bugs
Julian Scharf
Julian Scharf
2,007 Points

I have a related question about the syntax of this function.

This is what I see in the IDE autocomplete: todo.insert(newElement: Element, atIndex: Int)

typing atIndex: is mandatory

why don't we also need to type in newElement:?