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

jon kelson
jon kelson
5,149 Points

as far as I can see Ive done this correctly but the error message is asking me to do what ive done. please help

how is this wrong

arrays.swift
// Enter your code below
var arrayOfInts: [Int] = [1,2,3,4,5,6]

arrayOfInts.append(1,2)

arrayOfInts += [9,10]

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Basically, with this assignment you are asked to "...add two more values to our existing array." As we know two ways of appending elements to an array ('append' and concatenation), you are asked to append one element per approach.

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

Additionally, append lets you only add one element at a time, trying to add more than one will result in an error. If you wanted to append multiple elements at a time appendContentsOf comes in handy.

// This won't work, only one element
// can be appended at a time
arrayOfInts.append(7)

// This works
arrayOfInts.appendContentsOf([9, 10])

// This works as well
arrayOfInts += [9, 10]
Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I don't think you can pass more than one argument into the .append() method. Also, they've only asked for one of each kind - the append method and the concatenation way. Try this:

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

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