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 Introduction to Collections Working With Arrays

Ryan Rassoli
Ryan Rassoli
3,365 Points

Can anyone figure this out?

Hey, stuck on this question. Anyone find my error? Thanks

// Enter your code below
var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append = [7,8]
arrayOfInts += arrayOfInts + [1] 

1 Answer

28,558 Points

There are two issues:

  1. append is a method, so you have to call it using parenthesis. And the value you want to pass to it has to go inside of those parenthesis. Using the equals operator on it is invalid. It also only accepts one value at a time, so appending two numbers at once is not valid.
  2. The += operator is an operator that is a shorthand that adds whatever is on the right to the object, rather than replacing it. Since you include the object name on the right of the operator your code essentially translates to this: arrayOfInts = arrayOfInts + arrayOfInts + [1]. When appending to an object you should either use = and specify the name and the new value like you did (arrayOfInts = arrayOfInts + [1]) or use += and only specify the value you want to add (arrayOfInts += [1]), you should not do both which is essentially what you are doing.

If you fix those two issues like this:

var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(7) // Call append method and add only one number
arrayOfInts += [1] // Use += without adding variable name on right of operator

Then your code will pass task 2.