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

Sacha Karsenty
Sacha Karsenty
3,903 Points

Is it possible to add a whole array at a specific position in an array? And two other questions…

So we can use the compound operator "+=" to add an array to an array. We can also add an item to an array at a specific position using the ".insert method". But is it possible add a whole array at a specific position so a mix of both?

Second question is it possible to remove a whole array to an array? Third question is it possible to remove a an array in a array using a specific position? To exemplify this third question, if I had an array of 10 items and I wanted to delete items from 0-5 can I do that in a simple manipulation or do I have to use “removeAtIndex “6 times?

Thank you for any help, hope the questions are clear enough.

1 Answer

Joel Martin
Joel Martin
2,799 Points

Like the method ".insert" is used to insert a new element into an Array at a specific index, other methods can be used to do what you're looking for.

So in order : First question : ".insertContentsOf([yourArray], at: index)" method will be used.

Second question (which I understand as removing all the items in the array) : ".removeAll()"

Third question : ".removeRange(range)"

For more documentation about ranges : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html Under the section "Range Operators" (roughly 2/3 in).

Sacha Karsenty
Sacha Karsenty
3,903 Points

Thank you very much Joel, both for the answers and the link to the documentation.