我正在学习Big Nerd Ranch的Swift编程书(第二版),在Switch语句的章节中,有一小节关于in-cases以及如何使用它们。在描述如何使用多个条件来实现if-cases时,书中展示了以下代码:
...
let age = 25
if case 18...35 = age, age >= 21 {
print("In cool demographic and of drinking age")
}
当我在我的Xcode playground中尝试按照原样实现时,却出现了一个错误("variable binding in a condition requires an initializer")。
看起来是 age >= 21 这部分引起了实际的问题。
let age = 25
if case 18...35 = age{
// Same thing
}
工作得很好。在多条件代码中我做错了什么?
if case 18...35 = age where age >= 21 {
。 - vacawama