A Swift Tour, Control Flow error

I am going through the Swift Tour and found an error in the second code snippet under Control Flow.

Current

var teamScore = 11
let scoreDecoration = if teamScore > 10 {
    "🎉"
} else {
    ""
}

Error: Consecutive statements on a line must be separated by ';'

Proposed

var teamScore = 11
let scoreDecoration = teamScore > 10 ? "🎉" : ""
Answered by Claude31 in 760236022

Xcode 14.3. 1 uses Swift 5.8.1, not 5.9.

so this syntax is not yet supported and hence logically causing the error you get.

Which version of Xcode do you use ?

this syntax was introduced in Swift 5.9: https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md

But in this case, your proposal is just simpler to read and shorter.

I am using Xcode 14.3.1

Are you able to reproduce the error using the example in the Control Flow documentation?

Accepted Answer

Xcode 14.3. 1 uses Swift 5.8.1, not 5.9.

so this syntax is not yet supported and hence logically causing the error you get.

Just for the fun of it, with older Swift, you can use a closure to get nearly the same:

var teamScore = 11
let scoreDecoration = { if teamScore > 10 {
    return "🎉"
} else {
    return ""
}
}()

Nice! Thanks Claude.

A Swift Tour, Control Flow error
 
 
Q