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 Control Flow With Loops Working With Loops

how do I retrieve the numbers from the array?

.

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter < numbers.count{
sum.append(numbers)
sum+=numbers
counter += 1
}

1 Answer

Trevor Duersch
Trevor Duersch
9,964 Points

Bob, You are pretty close. You are first calling "sum.append(numbers)". To me, it looks like you are trying to sum up each number in the numbers array to your sum variable. The "append" function is used for arrays or for strings, but not for an Integer. What you want to do is iterate through each index in your array by use of the square brackets [ ]. Your counter variable is used for iterating through each index in your array, so you want to have that help you get each value in the array. I changed 2 lines of your code so I could get the correct value -

Old Code:

sum.append(numbers)
sum+=numbers

New Code:

sum += numbers[counter]

Keep practicing in your playground in Xcode. I would also review the videos on arrays and how to access each value. I hope that helps! :)