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 Control Flow With Loops For In Loops

Append a value to an array

I don't know how to append the value to the results array! var results: [Int] = [1,2,3,4,5,6,7,8,9,10] is not correct?

1 Answer

28,558 Points

Appending a value means to add one after the array has been created, in the example you post you are setting it to a specific value when it is created, which is not the same thing.

Appending to an array is actually pretty simple though, arrrays have a method called append that does exactly what the name suggests.


var results: [Int] = []

results.append(5) // You can type pure values
// results is now equal to [5]

results.append(5 + 2) // You can also type equations and other expressions like that
// results is now equal to [5, 7]