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

Mutating an array

In my todo array, when I mutate the index value 4, I'm changing the fifth element..

So first my array is like that ["Finish collections course", "Buy grocieries", "Respond to emails", "Pick up dry cleaning", "Order book online"]

Then todo[4] = "Brush teeth"

["Finish collections course", "Buy grocieries", "Respond to emails", "Pick up dry cleaning", "Brush teeth"]

Sorry, but I'm a little bit confused, even if I have understood of it works! Thanks

Giovanni

1 Answer

Ian Billings
PLUS
Ian Billings
Courses Plus Student 7,494 Points

Hi Giovanni,

Its because Arrays always start at index 0, so "Finish collections course" would be referred to as todo[0]. If you tried to access todo[5] in your example you would throw an Array Index Out of Bounds error as that item doesn't actually exist.

Hope that makes sense.

Ian

Absolutely yes! Thanks

Giovanni