在 Swift 中使用 switch 语句重构 IF-Else 嵌套代码

Refactor IF-Else nested code with switch statement in Swift

我正在努力改进我的 swift 编码。

我有一个完全可用的 if-else 嵌套函数,我相信可以使用 Switch 语句对其进行更好的优化。 有人可以建议进行此代码重构的正确方法吗?

这是我的工作代码:

    if firstButton.frame.contains(location) {
        UIView.animate(withDuration: 0.4) {
            self.firstButton.transform = CGAffineTransform(scaleX: -1, y: 1)
            self.firstButton.alpha = 0
                 if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
            else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.lastLetterTapped()}
        }
    } else
        if secondButton.frame.contains(location) {
            UIView.animate(withDuration: 0.4) {
                self.secondButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                self.secondButton.alpha = 0
                     if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
                else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.lastLetterTapped()}
            }
    } else
            if thirdButton.frame.contains(location) {
                UIView.animate(withDuration: 0.4) {
                    self.thirdButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                    self.thirdButton.alpha = 0
                         if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
                    else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.lastLetterTapped()}
                }
            } else
                if fourthButton.frame.contains(location) {
                    UIView.animate(withDuration: 0.4) {
                        self.fourthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                        self.fourthButton.alpha = 0
                             if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
                        else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.lastLetterTapped()}
                    }
                } else
                    if fifthButton.frame.contains(location) {
                        UIView.animate(withDuration: 0.4) {
                            self.fifthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
                            self.fifthButton.alpha = 0
                                 if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
                            else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.lastLetterTapped()}
                        }

    }

这不是优化代码,但像下面的代码一样,将重复的操作代码合并为函数

func anything (button: UIButton) {
    UIView.animate(withDuration: 0.4) {[weak self] in
    guard let `self` = self else { return }

    self.button.transform = CGAffineTransform(scaleX: -1, y: 1)
    self.button.alpha = 0

    switch self.tapCount {
      case 0: 
        self.placeholderText1.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 1: 
        self.placeholderText2.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 2: 
        self.placeholderText3.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 3: 
        self.placeholderText4.textWithAnimation(text: button.text!, duration: 0.3)
        self.tapCount += 1
      case 4: 
        self.placeholderText5.textWithAnimation(text: button.text!, duration: 0.3)
        self.lastLetterTapped()
    }
}

// 
guard let foundButton = [
  firstButton, secondButton, thirdButton, fourthButton, fifthButton
].first{ [=10=].frame.contains(location) } else { return }

anything(button: foundButton)

您可以将代码重写为,

if let locationView = [firstButton, secondButton, thirdButton, fourthButton].first(where: {[=10=].frame.contains(location)}) {
    UIView.animate(withDuration: 0.4) {[weak self] in
        guard let `self` = self else {
            return
        }

        button.transform = CGAffineTransform(scaleX: -1, y: 1)
        button.alpha = 0

        var placeholderText: UITextInput? //change the type to whatever type of placeholderText1 and others
        switch self.tapCount {
        case 0:
            placeholderText = self.placeholderText1
        case 1:
            placeholderText = self.placeholderText2
        case 2:
            placeholderText = self.placeholderText3
        case 3:
            placeholderText = self.placeholderText4
        case 4:
            placeholderText = self.placeholderText5
            self.lastLetterTapped()
        default:
            break
        }
        if let text = placeholderText,  {
            text.textWithAnimation(text: button.text, duration: 0.3)
            if self.tapCount != 4 {
                self.tapCount += 1
            }
        }
    }
}

在上面的代码中,

var placeholderText: UITextInput? //change the type to whatever type of placeholderText1 and others

UITextInput替换为placeholderText1的类型。