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

stuck and I don't know why?

Hi, I tried let item = todo.removeAtIndex(2) and als

let item = todo.removeLast and I'm not sure what's wrong with this code? thanks

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

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

let item = todo.removeAtIndex(2)
item

3 Answers

Hi Peter,

You're doing great :)

On task 2 / 3, when I use your code, I get the following compiler error - which you can also see by click the preview button.

swift_lint.swift:7:1: error: expression resolves to an unused l-value

todo

^~~~

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

todo.append("Debug App")
todo.append("Fix Bugs")
todo  // the compiler is complaining about this line. it doesn't know what you're trying to do with todo.

let item = todo.removeAtIndex(2)
item // this line will be another similar error as above

After removing those two lines, the code will pass and you'll get to task 3 / 3.

Cheers

Hi Robert Thanks for your help and encouragement! I wrote 'todo' and 'item' thinking that this is what's required to CALL the preceding code. I've come across the term 'call' a few times and I thought I've seen it in the current video. Clearly I'm not sure what I'm doing. Thanks again and good night from this part of the world. cheers

You've been calling methods all along and may not have realized it.

Each time you write .append(), you're calling the 'append' method. Same with .removeAtIndex().

The variable todo is an object holding an array. Because it contains an array, you can call array methods on todo like 'append' and 'removeAtIndex'.

Just to recap, when told to call 'append' on 'todo', this works because 'append' is a method that works on arrays, and 'todo' contains an array.

Hope that helps,

Cheers

Yes, great explanations that really help a newbie a lot. It will take some time to sink in but I'm starting to get the picture bit by bit. I'm finding this forum and people like yourself of great value. Thanks again!