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 Collections and Control Flow Introduction to Collections Reading and Modifying an Array

Modifying values in array

In the video at 6:53 after todo[4] = "Brush teeth" the value of 4 is not only replaced but "Order book online" is pushed to what is now element 5 but when todo[0] = "Watch Treehouse content" the value of 0 is just replaced, "Finish collections course" doesn't get shifted

when I do this it only replaces the value of 4 but it doesn't extend "Order book online" on to a new element

Stephen Emery
Stephen Emery
14,384 Points

What is happening is that todo[4] and todo[5] are both equal to "Order book online." See below. So there is no pushing happening. You change todo[4] and todo[5] stays intact giving the impression todo[4] has moved.

Two of the same todos are created on the lines 14 -17 (from the lesson): // Concatenate array containing string literal to todo todo = todo + ["Order book online"] // Performing the same operation using the unary addition operator todo += ["Order book online"]

If you do a print(todo) on the line after "todo += ["Order book online"]", you will see this: ["Finish collections course", "Buy groceries", "Respond to emails", "Pick up dry cleaning", "Order book online", "Order book online"]

Hope that helps!

1 Answer

Rick Gleitz
Rick Gleitz
47,876 Points

I had the same question. Thanks for that answer, @Stephen Emery.