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

Variable has the wrong output to it, objective says to insert an element at the second position of my array.

So I put todo.insert("Learn iOS", atIndex: 1) and it says i have the wrong output. I don't understand what I might've messed up. Can someone point me in the right direction?

obey me
obey me
1,135 Points

what is the question asked ?

obey me,

The question links to the challenge in the "Related content" section in the right column.

5 Answers

Hi Paul,

The code you have for task 3 is fine. Can you post all the code that you have?

You want to make sure that you've kept all the previous code in place.

Also, you could refresh the challenge page and try again.

In task 2 you were supposed to remove the 3rd element or the element at index 2. You removed at index 1.

For task 3 you are supposed to insert "Learn iOS" at the second position. That would be index 1. You are inserting at index 0.

obey me
obey me
1,135 Points

Basically if you are inserting a new item in the array i will consider don't make the array constant because if you are inserting you are modifying which is where will get an error

                            var hello = ["A","B","C"]
                           hello.insert("D",atIndex:3)

Here is the Question: Finally, we forgot to add that we need to learn iOS development prior to building an app. Insert a new item titled "Learn iOS" at the second position in the todo array. Here is my code:

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

This is the error: Bummer! Your 'todo' variable has the wrong value in it.

obey me
obey me
1,135 Points

var t = ["Learn Swift", "Build App", "Deploy App"]

                               t.append ("Debug App")
                           t.append ("Fix Bugs")

                         t.removeAtIndex (1)
                         let item = t.removeAtIndex (1)
                        t.insert("Learn iOS",atIndex:0)
                       t

// I mean your code is good i just you had every code on the same line

Haha it just copy and pasted that way. So by value im assuming that it means the location in my array correct?

obey me
obey me
1,135 Points

what you said