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 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,538 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.