Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.