An old C programmer could use some help with Swift.
I don't understanding something about the if-case syntax. E.g.:
if case 20...30 = age {
print ("in range.")
}
The case 20...30 = age
appears to be the conditional test for the if
statement. So I was initially confused to see the assignment operator ('=') used instead of a comparison operator ('==').
Ok, I thought to myself, that probably means the case
statement is actually a function call that returns a boolean value. The returned value will then satisfy the comparison test in the if
statement.
As an experiment, I tried treating the the case
statement like a regular conditional test and placed parentheses around it. Swift will happily accept if (x == 5)
or if (true)
. But if (case 20...30 = age)
generates an error. So the case
statement doesn't seem to behave like function.
I'm just curious to understand what's happening here. Any insight would be greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…