user3724487 user3724487 - 4 months ago 43
Swift Question

Swift 2: expression pattern of type 'Bool' cannot match values of type 'Int'

I'm doing this problem set "FizzBuzz", and my switch statement is giving me some problems, here's my code:

func fizzBuzz(n: Int) -> String {

switch n {
case n % 3 == 0: print("Fizz")
case n % 5 == 0: print("Buzz")
case n % 15 == 0:print("FizzBuzz")
}
return "\(n)"
}


If you could provide me with pointers / hints, instead of giving me the correct code, that would be swell :D
I'd prefer solving it myself, but a few hints could get me out of this hole.

Answer

Try using "case let where":

func fizzBuzz(n: Int) -> String {
    switch n {
    case let n where n % 3 == 0:
        print("Fizz")
    case let n where n % 5 == 0:
        print("Buzz")
    case let n where n % 15 == 0:
        print("FizzBuzz")
    default:
        print("none")
    }
    return "\(n)"
}
Comments