Skip to content

Instantly share code, notes, and snippets.

@d-date
Created February 8, 2017 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-date/259c1371737f2608d6cfbf7e161f19da to your computer and use it in GitHub Desktop.
Save d-date/259c1371737f2608d6cfbf7e161f19da to your computer and use it in GitHub Desktop.

より良くするために考えたいのが、 これだと

case .division:
  if secondValue != 0 {
    value = firstValue / secondValue
  }

if currentOperator == .division && secondValue == 0

で判定が二重になってしまうので、 これを回避するために、こんなふうにも書けます。

guard currentOperator == .division && secondValue != 0 else {
  label.text = "ERROR"
  firstValue = 0
  secondValue = 0
  currentOperator = .undefined
  return
}
 var value = 0
 switch currentOperator {
   case .addition:
       value = firstValue + secondValue
   case .subtraction:
       value = firstValue - secondValue
   case .multiplication:
       value = firstValue * secondValue
    case .division:
       value = firstValue / secondValue
     default:
        value = firstValue
   }
label.text = "\(value)"
        
firstValue = 0
secondValue = 0
currentOperator = .undefined
}

しかしこれでもまだ不十分。 gurad else 内部の処理と、一番下の処理をまとめたい。 なので、defer 文というのでまとめてみる。

 @IBAction func equalButtonTapped(_ sender: UIButton) {
        
        defer {
            firstValue = 0
            secondValue = 0
            currentOperator = .undefined
        }
        
        guard currentOperator == .division && secondValue != 0 else {
            label.text = "ERROR"
            return
        }
        
        var value = 0
        switch currentOperator {
            case .addition:
                value = firstValue + secondValue
            case .subtraction:
                value = firstValue - secondValue
            case .multiplication:
                value = firstValue * secondValue
            case .division:
                value = firstValue / secondValue
            default:
                value = firstValue
        }
        label.text = "\(value)"
    }

deferは、そのメソッドのスコープを抜けるときに一番最後に必ず呼ばれる処理ブロックを表す。(詳細Swift参照) これによって、正常に処理が進んで、メソッドのスコープから抜けるときも、guardから抜けるときも、最後に共通の処理が行われる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment