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

Difference between mutating an array and appending?

In the video "Reading and Modifying an Array" it was shown how to mutate an array to insert a new string literal to an array, at a certain position and shift the existing contents along:

todo[4] = "Brush teeth

But in the following video "Inserting and Deleting Values" the insert method is used to achieve the same thing:

todo.insert("Watch TV", at: 2)

2 Answers

This:

todo[4] = "Brush teeth

Actually modifies the element if the element exists.

This:

todo.insert("Watch TV", at: 2)

Adds an element at a particular index, but doesn't modify

I hope this helped :grin:

Happy C0D1NG! :tada:

:dizzy: ~Alex :dizzy:

I see, so the first example replaces.