Я создаю приложение-калькулятор SwiftUI и застрял на проблеме, что в симуляторе я просто получаю строковые числа с названиями кнопок операций. Мне нужно решить эту проблему и нажатием на кнопки операций и после этого на кнопке получить результат расчета. Я буду очень рад любой помощи.
import SwiftUI import UIKit
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 = "0"
@Published var userIsTyping = false
var storedNumber: Double?
var storedAction = false
var displayValue: Double {
get {
return Double(display)!
}
set {
display = String(newValue)
}
}
}
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: {
if self.actions.userIsTyping {
self.actions.display = self.actions.display + self.button.title } else {
self.actions.display = self.button.title
self.actions.userIsTyping = true
}
}) {
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
}
func performOperation(_ title: CalcButtons){
switch title {
case .rootExtraction:
self.actions.displayValue = sqrt(actions.displayValue)
print (actions.displayValue)
default:
break
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(DisplayingActions())
}
}