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

Nicklas Lind
Nicklas Lind
3,619 Points

Is this an error in the Arrays challenge? (Swift Basics)

Hello,

I was trying to solve the challenge:

"Now that we have to fix bugs in out app we cannot deploy it. Please remove the third item (β€œDeploy App”) and assign it to a constant named item."

I found a solution, but I don't understand why the number of the index is 1, not 2.

Here is the code and solution:

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append("Debug App")
todo.append("Fix bugs")

todo.removeAtIndex(1)
let item = todo.removeAtIndex(1)

I am aware that the index starts at 0, 1, 2, 3 etc, which is why I am confused :) Should the number in the removeAtIndex be 2?

Can someone tell me what's going on here?

Thanks in advance,

Nicklas

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Nicklas,

Let's start with the first issue which is you're removing the second value Build App from the array then removing Deploy App and assigning it correctly but because Build App no longer exists the challenge will fail, instead you just want to assign the value Deploy App to the constant item by selecting the 2nd index in the array which is the 3rd item as array's always start at zero as we know.

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append("Debug App")
todo.append("Fix bugs")

let item = todo.removeAtIndex(2)

You can also do the following instead of appending each item manually...

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

So let's put the indexes into perspective.

var todo = [
  "Learn Swift", // 0
  "Build App", // 1
  "Deploy App", // 2
  "Debug App", // 3
  "Fix bugs" // 4
]

The easiest way I always find to remember which index something lives at is by counting from one and then misusing one less from the total instead of counting from zero which can get confusing, it just comes down to which you prefer to remember the index.

Hope that helps.

gabriel siqueira
gabriel siqueira
7,405 Points

From Chris explanation the code should be

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

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

todo.removeAtIndex(3)

let item = todo.removeAtIndex(3)

But for the challenge to work you have to use the one Nicklas used

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

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

todo.removeAtIndex(1)

let item = todo.removeAtIndex(1)

That got me confused as well...

Yusuf Diyab
Yusuf Diyab
2,485 Points
var todo = ["Learn Swift", "Build App", "Deploy App"]

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

let item = todo.removeAtIndex[2]

Still do get this error Bummer! You need to call the 'removeAtIndex' method on the array.