我定义了一个执行多个 if 语句的函数。尽管它在定义它的流中只执行 if 语句而不执行 else 语句。我必须在相关 if 语句的相应调用之后执行 else 语句。
func levelTwo() {
if firstText.text == "A"
{
hintLabel.text = "\(self.hintsTwo[1])"
self.firstValid.image = #imageLiteral(resourceName: "check")
}
else {
self.firstValid.image = #imageLiteral(resourceName: "markQ")
}
if SecFirstText.text == "A" && secSecText.text == "C"
{
hintLabel.text = "\(self.hintsTwo[2])"
self.secondValid.image = #imageLiteral(resourceName: "check")
}
else {
self.secondValid.image = #imageLiteral(resourceName: "markQ")
}
if thiFirstText.text == "C" && thiSecText.text == "A" && thiThirdText.text == "T"
{
hintLabel.text = "\(self.hintsTwo[3])"
self.thirdValid.image = #imageLiteral(resourceName: "check")
}
else {
self.thirdValid.image = #imageLiteral(resourceName: "markQ")
}
}
您可以将 switch 与元组一起使用:
switch(firstText.text, thiSecText.text) {
case ("A", _):
//First text equal a, second text equal anything
break
case ("A", "C"):
//First text A, second text B
break
case (_, "B"):
//First text anything, second text B
break
default:
break
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句