【发布时间】:2018-01-27 11:16:46
【问题描述】:
我正在尝试同时使用两个变量来获得一个简单的 switch 语句,但是我不断收到大量错误,而且我真的不知道如何正确地使用它。我只想知道一个是/不是零,另一个是或不是零。我的代码如下:
var ptsRemaining: Int?
var ptsUsed: Int = 0
func updatedButtons() {
switch (ptsRemaining, ptsUsed) {
case (0), (0):
print("do thing 1")
case (0), (!0):
print("do thing 2")
case (!0), (0):
print("do thing 3")
case (!0), (!0):
print("do thing 4")
default:
print("error!")
}
}
注意我也试过这个:
func updatedButtons() {
switch (ptsRemaining, ptsUsed) {
case (0,0):
print("hello")
case (0,!0):
print("hello")
case (!0,0):
print("hello")
case (!0,!0):
print("hello")
default:
print("Error")
}
}
在这两种情况下,我收到的错误如下:
Cannot convert value of type 'Int' to expected argument type 'Bool'
Expression pattern of type 'Int' cannot match values of type 'Int?'
【问题讨论】:
-
不是
case (0, 0):吗?此外,您在print("error!"处缺少右括号 -
我正在查看这个问题*.com/questions/25165123/…,他们做的是 (thing), (thing) 而不是 (thing, thing) 。无论如何都尝试过,但仍然出现错误
-
@dvd.Void 如果你这样写,逗号的意思是“或”。