title: Swiftui Calculator Continuous calculation
body: <p>I want to practice the real ios calculator in my app and can calculate continuously</p>
  <p>When I tap add button, I want to can be calculated.But now my value can't really be calculated automatically when I tap add button.I don't have any idea about this issue.</p>
  <p>How should I change my code?</p>
  <p>here's my ContentView.swift:</p>
  <pre><code>import SwiftUI
  
  enum CalcButton: String {
      case one = &quot;1&quot;
      case two = &quot;2&quot;
      case three = &quot;3&quot;
      case four = &quot;4&quot;
      case five = &quot;5&quot;
      case six = &quot;6&quot;
      case seven = &quot;7&quot;
      case eight = &quot;8&quot;
      case nine = &quot;9&quot;
      case zero = &quot;0&quot;
      case add = &quot;+&quot;
      case subtract = &quot;-&quot;
      case divide = &quot;รท&quot;
      case mutliply = &quot;x&quot;
      case equal = &quot;=&quot;
      case clear = &quot;AC&quot;
      case decimal = &quot;.&quot;
      case percent = &quot;%&quot;
      case negative = &quot;-/+&quot;
  
      var buttonColor: Color {
          switch self {
          case .add, .subtract, .mutliply, .divide, .equal:
              return .orange
          case .clear, .negative, .percent:
              return Color(.lightGray)
          default:
              return Color(UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1))
          }
      }
  }
  
  enum Operation {
      case add, subtract, multiply, divide, none
  }
  
  struct ContentView: View {
  
      @State var value = &quot;0&quot;
      @State var runningNumber = 0
      @State var currentOperation: Operation = .none
  
      let buttons: [[CalcButton]] = [
          [.clear, .negative, .percent, .divide],
          [.seven, .eight, .nine, .mutliply],
          [.four, .five, .six, .subtract],
          [.one, .two, .three, .add],
          [.zero, .decimal, .equal],
      ]
  
      var body: some View {
          ZStack {
              Color.black.edgesIgnoringSafeArea(.all)
  
              VStack {
                  Spacer()
  
                  // Text display
                  HStack {
                      Spacer()
                      Text(value)
                          .bold()
                          .font(.system(size: 100))
                          .foregroundColor(.white)
                  }
                  .padding()
  
                  // Our buttons
                  ForEach(buttons, id: \.self) { row in
                      HStack(spacing: 12) {
                          ForEach(row, id: \.self) { item in
                              Button(action: {
                                  self.didTap(button: item)
                              }, label: {
                                  Text(item.rawValue)
                                      .font(.system(size: 32))
                                      .frame(
                                          width: self.buttonWidth(item: item),
                                          height: self.buttonHeight()
                                      )
                                      .background(item.buttonColor)
                                      .foregroundColor(.white)
                                      .cornerRadius(self.buttonWidth(item: item)/2)
                              })
                          }
                      }
                      .padding(.bottom, 3)
                  }
              }
          }
      }
  
      func didTap(button: CalcButton) {
          switch button {
          case .add, .subtract, .mutliply, .divide, .equal:
              if button == .add {
                  self.currentOperation = .add
                  self.runningNumber = Int(self.value) ?? 0
                  self.value = &quot;\(runningNumber + Int(self.value) ?? 0)&quot;
                  
              }
              else if button == .subtract {
                  self.currentOperation = .subtract
                  self.runningNumber = Int(self.value) ?? 0
              }
              else if button == .mutliply {
                  self.currentOperation = .multiply
                  self.runningNumber = Int(self.value) ?? 0
              }
              else if button == .divide {
                  self.currentOperation = .divide
                  self.runningNumber = Int(self.value) ?? 0
              }
              else if button == .equal {
                  let runningValue = self.runningNumber
                  let currentValue = Int(self.value) ?? 0
                  switch self.currentOperation {
                  case .add: self.value = &quot;\(runningValue + currentValue)&quot;
                  case .subtract: self.value = &quot;\(runningValue - currentValue)&quot;
                  case .multiply: self.value = &quot;\(runningValue * currentValue)&quot;
                  case .divide: self.value = &quot;\(runningValue / currentValue)&quot;
                  case .none:
                      break
                  }
              }
  
              if button != .equal {
                  self.value = &quot;0&quot;
              }
          case .clear:
              self.value = &quot;0&quot;
          case .decimal, .negative, .percent:
              break
          default:
              let number = button.rawValue
              if self.value == &quot;0&quot; {
                  value = number
              }
              else {
                  self.value = &quot;\(self.value)\(number)&quot;
              }
          }
      }
  
      func buttonWidth(item: CalcButton) -&gt; CGFloat {
          if item == .zero {
              return ((UIScreen.main.bounds.width - (4*12)) / 4) * 2
          }
          return (UIScreen.main.bounds.width - (5*12)) / 4
      }
  
      func buttonHeight() -&gt; CGFloat {
          return (UIScreen.main.bounds.width - (5*12)) / 4
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>