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 Basics (retired) Control Flow While and Do-While Loop

Hey guys, really struggling here. Any ideas where I am going wrong?

var index = 0 while index < numbers { println(numbers[Index]) index++

Is there anything wrong with this?

while_loops.swift
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers {
             println(numbers[Index])
             index++

}

1 Answer

Gareth Borcherds
Gareth Borcherds
9,372 Points

numbers is a NSArray so when you try and compare it to index < numbers you aren't saying less than a number. You need to get the count of the array. Try this.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers.count {
    println(numbers[index])
    index++

}

it is still not working! I did try that previously but gave it another go when you recommended it!

Gareth Borcherds
Gareth Borcherds
9,372 Points

What error are you receiving? Also, you had an incorrect spelling of index where you had a capital I for Index in your print statement. I fixed that in my code above. I tried this in a playground and it's working.