我需要进行计算,但只得到字符串。我应该怎么算?

我需要进行计算,但是当点击按钮时,我在模拟器上得到的所有内容只是字符串而无需任何计算。我该如何解决。我该怎么办?所有按钮均已创建且运行完美。但是计算不起作用,并且在屏幕上我也有计算符号,但是我不需要显示它如何解决此问题?

import SwiftUI

enum CalcButtons: String {

case zero, one, two, three, four, five, six, seven, eight, nine
case equals, plus, minus, multiply, divide, decimal
case ac, minusPlus, percent, rootExtraction, factorial, tenInExponentiation, numberInExponentiation, deleteOneDigit

var title: String {
    switch self {
    case .zero: return "0"
    case .one: return "1"
    case .two: return "2"
    case .three: return "3"
    case .four: return "4"
    case .five: return "5"
    case .six: return "6"
    case .seven: return "7"
    case .eight: return "8"
    case .nine: return "9"
    case .minusPlus: return "+/-"
    case .percent: return "%"
    case .divide: return "÷"
    case .multiply: return "x"
    case .plus: return "+"
    case .minus: return "-"
    case .equals: return "="
    case .decimal: return "."
    case .deleteOneDigit: return "←"
    case .rootExtraction: return "√x"
    case .factorial: return "X!"
    case .tenInExponentiation: return "10ª"
    case .numberInExponentiation: return "X^10"
    default:
        return "AC"
    }
}

var backgroundButtonsColor: Color {
    switch self {
    case .zero, .one, .two, .three,.four, .five, .six, .seven, .eight, .nine, .decimal:
        return Color(.darkGray)
    case .ac, .minusPlus, .percent, .rootExtraction, .factorial, .tenInExponentiation, .numberInExponentiation, .deleteOneDigit:
        return Color(.lightGray)
    default:
        return .orange
    }
}

}


class DisplayingActions: ObservableObject {

@Published var display = ""

func displayAction(calculatorButtons: CalcButtons){

        self.display = self.display + calculatorButtons.title

    switch calculatorButtons {
    case .ac:
        return self.display = ""
    case .decimal:
        if self.display == calculatorButtons.title && calculatorButtons.title != "."{
            return self.display = self.display + "."
        } else { return
            self.display = "0."

        }
    default:
        break
    }

}


}

struct ContentView: View {

@EnvironmentObject var actions: DisplayingActions

let buttons: [[CalcButtons]] = [
    [.ac, .minusPlus, .percent, .divide, .deleteOneDigit],
    [.seven, .eight, .nine, .multiply, .rootExtraction],
    [.four, .five, .six, .minus, .numberInExponentiation],
    [.one, .two, .three, .plus, .tenInExponentiation],
    [.zero, .decimal, .equals, .factorial]]

var body: some View {

    ZStack (alignment: .bottom) {
        Color.black.edgesIgnoringSafeArea(.all)

        VStack(spacing: 3) {

            HStack {
                Spacer()
                Text(actions.display)
                    .foregroundColor(.white)
                    .font(.system(size: 62))
            }.padding()


            ForEach(buttons, id: \.self) { row in
                      HStack (spacing: 3) {
                      ForEach(row, id: \.self) {button in
                        CalculatorButtonsView(button: button)


                      }

                      }
            }
        }.padding(.bottom)
    }
}

}

struct CalculatorButtonsView: View {

var button: CalcButtons

@EnvironmentObject var actions: DisplayingActions

var body: some View{
    Button(action: {

        self.actions.displayAction(calculatorButtons: self.button)
    }) {
        Text(button.title)
       .font(.system(size: 30))
            .frame(width: buttonWidth(button: button), height: (UIScreen.main.bounds.width - 5 * 3 ) / 5)
       .foregroundColor(.white)
       .background(button.backgroundButtonsColor)
            .cornerRadius(buttonWidth(button: button))

    }
}
private  func buttonWidth(button: CalcButtons) -> CGFloat {

    if button == .zero {
        return (UIScreen.main.bounds.width - 4 * 3 ) / 4 * 1.6
    }
      return (UIScreen.main.bounds.width - 5 * 3 ) / 5
  }
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
    ContentView().environmentObject(DisplayingActions())
}

}

评论