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

Travis Trevisan
Travis Trevisan
5,846 Points

having problems passing the test, here is my code. todo.insert("Learn iOS", atIndex: 2) todo

// Playground - noun: a place where people can play

import UIKit

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

let item = todo.removeAtIndex(2) item

todo.insert("Learn iOS", atIndex: 2) todo

//it works in Xcode, just not on the site?

arrays.swift
var todo = ["Learn Swift", "Build App", "Deploy App", "Debug App", "Fix Bugs"]

let item = todo.removeAtIndex(2)
item

todo.insert("Learn iOS", atIndex: 2)
todo
Alex Lobert
Alex Lobert
2,736 Points

Hi Travis,

Two thoughts:

  1. if it let you get to third piece of the challenge, your issue is that you aren't assigning "Learn iOS to the 2nd position, you are are assigning it to the third - arrays start counting at 0 not 1.

todo.insert("Learn iOS", atIndex: 1)

  1. Additionally thought... when adding items to an array, you probably want to use an operator like "+=" the addition assignment operator to do this instead of rewriting the array.

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

Hope that helps.

-Alex

2 Answers

Hello Travis Trevisan. I am "sorry" if I misunderstood you. I think you forgot the "colon" in the first line. Here is my solution for you. I hope it helps you!

var todo: [String] = ["Learn Swift", "Build App", "Deploy App", "Debug App", "Fix Bugs"]

let item = todo.removeAtIndex(2) item

todo.insert("Learn iOS", atIndex: 2) todo

john

Travis Trevisan
Travis Trevisan
5,846 Points

thanks John and Alex!

John I've been using the same code that you demonstrated, no luck, I'm just going to move on, because the code I'm using works in Xcode!

Thanks for the help! - Travis

Okay, I am "sorry". Good luck!