如何在Switch语句中查看下一个案例?

时间:2015-08-17 03:49:42

标签: ios swift

在Swift中fallthrough之后有没有办法检查下一个案例?看起来这是编写简洁代码的好方法。

    case 1...8:

        self.correctAnswerLabelOne.text = answerLabelOne?.firstObject as? String
        self.correctAnswerLabelOne.alpha = 0.0
        self.correctAnswerLabelOne.hidden = false
        self.fadeAnimation(self.correctAnswerLabelOne, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

        fallthrough

    case 2...8:

        self.correctAnswerLabelTwo.text = answerLabelTwo?.firstObject as? String
        self.correctAnswerLabelTwo.alpha = 0.0
        self.correctAnswerLabelTwo.hidden = false
        self.fadeAnimation(self.correctAnswerLabelTwo, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

        fallthrough

    case 3...8:

        self.correctAnswerLabelThree.text = answerLabelThree?.firstObject as? String
        self.correctAnswerLabelThree.alpha = 0.0
        self.correctAnswerLabelThree.hidden = false
        self.fadeAnimation(self.correctAnswerLabelThree, duration: 0.3, delay:     0.0, alpha: 1.0, options: .CurveEaseIn)

如果case为3:我不想将代码从case 1case 2添加到case 3我想激活第一个案例,第二个案例,然后是第三种情况。我可以在每个案例中使用if-statement,但这似乎不是最好的方法。是否有其他类型的控制流可以更好地适应这一目的,还是只需要将代码一直复制到一起?

在交换机内部,continue对我来说,似乎是一个合理的候选者,可以触发Switch继续检查案例陈述的业务。

更新

if-statement添加到前提条件fallthrough完成工作。它仍然不如我想象的那么“优雅”,但它有效......

    switch(numberOfAnswers) {

    case 1...8:

        self.correctAnswerLabelOne.text = answerLabelOne?.firstObject as? String
        self.correctAnswerLabelOne.alpha = 0.0
        self.correctAnswerLabelOne.hidden = false
        self.fadeAnimation(self.correctAnswerLabelOne, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

        if numberOfAnswers > 1 {

            fallthrough

        }

    case 2...8:

        self.correctAnswerLabelTwo.text = answerLabelTwo?.firstObject as? String
        self.correctAnswerLabelTwo.alpha = 0.0
        self.correctAnswerLabelTwo.hidden = false

        self.fadeAnimation(self.correctAnswerLabelTwo, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

        if numberOfAnswers > 2 {

            fallthrough

        }

2 个答案:

答案 0 :(得分:0)

如果switch语句被反转,你的代码会工作吗?例如

switch(numberOfAnswers) {

case 2...8:

    self.correctAnswerLabelTwo.text = answerLabelTwo?.firstObject as? String
    self.correctAnswerLabelTwo.alpha = 0.0
    self.correctAnswerLabelTwo.hidden = false

    self.fadeAnimation(self.correctAnswerLabelTwo, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

    if numberOfAnswers > 2 {

        fallthrough

    }

case 1...8:

    self.correctAnswerLabelOne.text = answerLabelOne?.firstObject as? String
    self.correctAnswerLabelOne.alpha = 0.0
    self.correctAnswerLabelOne.hidden = false
    self.fadeAnimation(self.correctAnswerLabelOne, duration: 0.3, delay: 0.0, alpha: 1.0, options: .CurveEaseIn)

    if numberOfAnswers > 1 {

        fallthrough

    }

然后对每个案例的评估并不重要,因为上述案例的范围无论如何都包含在案例范围内

答案 1 :(得分:0)

不要使用self.correctAnswerLabelOnecorrectAnswerLabelTwo等,而是制作这些标签的列表,以便您可以将其作为self.correctAnswerLabel[i]来解决。如果这些是在Interface Builder中单独布局的,那么您可以在viewDidLoad期间设置该数组。

一旦他们进入列表,所有这些逻辑变得更加容易。你可以做类似的事情:

for (idx, label) in self.correctAnswerLabel.enumerate() {
    if idx > numberOfAnswers {
       // Hide label
    }
    else {
       // Show label
    }
}