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

Osiris Maldonado
Osiris Maldonado
699 Points

Can an array of items be added with the .append function?

I know that an array of new items can be appended using the +=

todo += ["Additional Stuff", "More Stuff"]

But can an array be added using .append? or can .append achieve just single elements?

Thanks in advance!

2 Answers

The .append call is expecting a single element. If you have an array of strings and try to .append another array of strings you'll get, '[String]' is not convertible to String

Osiris Maldonado
Osiris Maldonado
699 Points

Ok, I see, so .append is only useful for adding single elements. I assume that to add multiple elements, the best bet is to use '+=' ?

I guess so, yes. Or you could use a for loop to iterate through the second array and append each element one by one, perhaps?