While following the swift tour documentation i came across this block of code which is supposed to conditionally return a value, then gets stored in a variable. But then the compiler throws me an error.
Consecutive statements on a line must be separated by ';'
my code :
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
let scoreDecoration = if teamScore > 10 {
"🎉"
} else {
""
}
print("Score:", teamScore, scoreDecoration)
Screenshot
You cannot add the if as you did.
But you can replace with so called ternary operator:
let scoreDecoration = teamScore > 10 ? "🎉" : ""
If you do want to use the if, create a computed var:
var scoreDecoration : String {
if teamScore > 10 {
return "🎉"
} else {
return ""
}
}