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 Basics (retired) Collections Modifying an Array

Michael McKenna
Michael McKenna
3,451 Points

What would be an efficient way of inserting a String into a long list?

It looks like all of the items in the list are shifting after inserting a new item at the beginning. Is there a different/more dynamic way of inserting a new variable? I feel like this method wouldn't be smooth if there was a list of thousands of items.

1 Answer

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi Michael

Generally, I don't think arrays would be suited for thousands of items. But if you are asking for an easier way to append values - the += operator can be used to add lot's and lot's more strings to your array.

If that's not what you're looking for, let me know.

Michael McKenna
Michael McKenna
3,451 Points

Thanks for the "swift" reply. Sorry I've been waiting to use that.

Jokes aside, what data structure would be appropriate for a list of thousands of items in Swift?

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hahaha, good one! :-D

Well, I would assume a dictionary would be a better fit for that.. Depending on what you want to make this huge list of? Usually you would pull data from the internet to your app - in which case you could store it in a dictionary and add a new key-value pair each time the data comes in. But I'm not an expert - maybe an array is okay to use for this.. I just assume not, since it's ordered, and you wouldn't have a clue which index to type if you need some specific data.