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

Congo Bergseteren
Congo Bergseteren
477 Points

Trying to figure out what I am doing wrong....

I feel like I am following all directions but I am returned with a "Bummer"

this is the assignment:

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.

arrays.swift
// Enter your code below

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

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

3 Answers

You seem to have it. My guess is they only wanted you to add one value using each method (append or subscript), as this works:

var arrayOfInts = [1,2,3,4,5,6]
arrayOfInts.append(9)
arrayOfInts += [7]
let value = arrayOfInts[4]
Congo Bergseteren
Congo Bergseteren
477 Points

Thanks a lot! That worked.

Wanted to ask one more question. Why is the last line true? Since arrays always start with 0, wouldn't that mean that the first number in the array (1) would be equal to 0? Which would cause the 5th number to be 6?

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Congo.

Your code is correct, except you added an extra line that wasn't asked for by the challenge.

It wanted only 1 value added with the append method and 1 value added with the concat. method. So, just delete the second append you have and you're good to go.

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

Keep Coding! :dizzy:

Congo, you asked: "Why is the last line true? Since arrays always start with 0, wouldn't that mean that the first number in the array (1) would be equal to 0? Which would cause the 5th number to be 6?"

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

The last line is neither true nor false. What it is doing is assigning the 5th value of the array to a constant named value. Re array indexing, arrayOfInts[0] is 1, arrayOfInts[1] is 2, arrayOfInts[2] is 3, etc. That is, since array indexing starts at 0, the 0th element, which is 1, as 0 as its index. The next element, 2, has 1 as its index, etc.

So, bottom line, arrayOfInts[5] is assigning the number 6 to value.

Yell if this still doesn't make sense!