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

using let

Why did we use let item = todo.removeLast() insted of just todo.removelast() ?

because item is a new constant that capture the value that is being remove from var todo. For example: [ Let item = todo.removeLast()] means "Declare a constant named item and assign to it the value that is being remove from the todo variable." Make sense??? Think so, other wise i strongly recommend to watch the video again before you continue

3 Answers

As it says in the Apple docs:

mutating func removeLast() -> T

You can see that the function returns the element that has been removed (T is a generic, as an array can contain elements of any type). So as Josue Gisber pointed out, you can assign the result of removeLast() to a variable/constant.

You don't have to, however, so this is all valid code:

var array = ["one", "two", "three"]

// Removes "three"
array.removeLast()

// Removes "two", assigns it to `lastRemovedElement` and logs it
let lastRemovedElement = array.removeLast()
print("Removed \(lastRemovedElement) from array")

When you add the let item = to the front of todo.removeLast(), you are assigning the last item in the todo array to the constant item; whereas if you just did todo.removeLast(), you would just remove the last item from the todo array

Thank you all. I get it know. As Martin Wildfeuer pointed in his example, I still can use that var if i assign it as a constant.

I´m from Angola, and portuguese is my native language. So it´s not easy to me to assimilate, that is way i wacth the videos at least 2 times.

Thanks.

Hey António! You are welcome! Keep up the learning, you are on the right track!