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 2.0 Collections and Control Flow Introduction to Collections Working with Arrays

Concatenating

The second questions wants an array by concatenating. Not sure why it doesn't work. On Playground, arrayOfInts += (7,8) works.

Challenge Task 2 of 4

Now that we have an array declared, since it is a mutable array, we can add and remove items. Let's start by adding two more values to our existing array. There are two ways we can add items to an array and I want you to give both a try. Add one item to the array using the append method.

Remember: We invoke methods on an array using a period or dot. After the dot we write out the method name and a value to append in parentheses following the name.

Add another item to the array by concatenating an array. When concatenating, assign the results of the expression back to arrayOfInts. /Users/mattconway/Desktop/Screen Shot 2016-01-09 at 3.53.04 PM.png

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

3 Answers

Hi mattconway,

You can use +=, but you'd want to use brackets, not parentheses.

var arrayOfInts = [1,2,3,4,5,6] 

arrayOfInts.append(10)
arrayOfInts += [7]

Hope that helps!

Still doesn't work. The instructions want concatenating which is probably x + y = z. += works in Playground, but not in the challenge task.

I just double checked to be sure; copying and pasting my code above will pass Task 1 and Task 2 of the Challenge, with Task 2 being the portion requiring array concatenation.

It works, thanks, but with your numbers. I had 1,2,3,4,5,6 as the variable. I appended two numbers, 7 and 8 on a separate line each. Then changed to 9 for +=, nothing worked. When I changed it to 10 for your append and 7 for the +=, it worked. I don't understand why it works for your numbers but not mine? Can you explain, if you don't mind. Thank you!

Hi Matt,

I think I understand the issue now; assuming the code you're using is as follows:

var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(7)
arrayOfInts.append(8)
arrayOfInts += [9, 10]

It wouldn't pass the Challenge because Task 2 specifically requests that only one item be added to the array via append() and one via concatenation.

However, for general application outside the Challenge, I don't see any issues.