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 If Statement

Paul Cullen
Paul Cullen
1,414 Points

Editor not working right?

Hey, ive written 2 versions of this code, both come up with the error "You must use an if statement" however they compile fine in xcode, both iterations are below, can you see an issue?

let months = [1, 2, 3]

for month in months { if(month == 1) { println("January"); } if(month == 2) { println("February"); } if(month == 3) { println("March"); } }

for i in months { if(i == 1) { println("January"); } if(i == 2) { println("February"); } if(i == 3) { println("March"); } }

months.swift
let months = [1, 2, 3]

for i in months {
 if(i == 1) {
  println("January");
 }
 if(i == 2) {
  println("February");
 }
 if(i == 3) {
  println("March");
 }
}

3 Answers

Hi Paul,

Whislt I don't think it would cause a problem, your code doesn't need the semi-colons at the end of each line. However, this code works:

let months = [1, 2, 3]

for month in months {
  if month == 1 {
    println("January")
  }
  if month == 2 {
    println("February")
  }
  if month == 3 {
    println("March")
  }

}

The compiler was playing up intermittently yesterday - it wanted incorrect code entering first (add a couple of incorrect brackets to your code) before it would pass the subsequently corrected code. I don't know if that persists today.

Steve.

It seems to be the brackets around your conditional tests that are failing the compiler tests. Not sure I agree with that - it shouldn't make a difference.

EDIT No it isn't - it is the lack of space between the if keyword and the bracket. Again, not sure I agree with that!!

Paul Cullen
Paul Cullen
1,414 Points

Thats some old habits from my PHP syntax, will need to try and forget about using them. Thanks Steve.

I always use too many code ornaments like that - it just reads more easily to my little brain!

Paul Cullen
Paul Cullen
1,414 Points

ha! glad im not the only one