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

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

getting an error on Xcode

Hi, I have a questions, when I input let numbers = [2,8,1,16,4,3,9] var sum = 0 var counter = 0

while counter < numbers.count { sum += numbers[counter] counter++ } on Xcode but it return this Untitled Page[30758] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

is this an error?

Thank you in advance

It looks like you have not set up you while loop correctly, but the rest looks good. Give me a shout if you need a better explanation on why the code below works.

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

// Enter your code below

while (counter < numbers.count){

sum += numbers[counter]
counter++
}

3 Answers

It looks like you have not set up you while loop correctly, but the rest looks good. Give me a shout if you need a better explanation on why the code below works.

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

// Enter your code below

while (counter < numbers.count){

sum += numbers[counter] counter++ }

Reagen Rahardjo
Reagen Rahardjo
3,770 Points

Hi,

I tried to input that to Xcode but still return an error

while (counter

sum += numbers[counter] counter++ }

can you explain it to me why my code is wrong.. im getting confuse because my code got error in Xcode but i passed the test using the code I posted earlier

You missed the closing bracket on the while (counter) . That's my fault though, hopefully this makes it easier to read.

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

// Enter your code below

while (counter){ 

sum += numbers[counter] 
counter++ 
}