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