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

Sam Belcher
Sam Belcher
3,719 Points

How am I not using the append method correctly??

My code looks just like the example

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

3 Answers

andren
andren
28,538 Points

You are using the append method correctly, the error messages for these challenges can often be pretty misleading. That's not to say there is nothing wrong with your solution though, there are two issues. One with the line that declares the variable and one with the line that concatenates 8 to it.

The issue with your declaration is that you define the variable type as being Int, but it is not in fact an Int. It is an array of ints, which has a type of [Int] (the brackets are important).

Similarly when you concatenate something to an array the thing you are concatenating to it also has to be an array. 8 by itself is not an array, but it can be made into an array by simply wrapping it in brackets.

If you fix those errors like this:

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

Then your code will work.

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

You just needed to add the square brackets around the Int type and the around the 8 you are adding to the array.