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 trialVunge António
1,096 Pointsusing let
Why did we use let item = todo.removeLast() insted of just todo.removelast() ?
3 Answers
Martin Wildfeuer
Courses Plus Student 11,071 PointsAs 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")
Caleb Kleveter
Treehouse Moderator 37,862 PointsWhen 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
Vunge António
1,096 PointsThank 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.
Martin Wildfeuer
Courses Plus Student 11,071 PointsHey António! You are welcome! Keep up the learning, you are on the right track!
Josue Gisber
Courses Plus Student 9,332 PointsJosue Gisber
Courses Plus Student 9,332 Pointsbecause 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