I have just started learning the basics of swift and was playing around with swiftui to make a calculator to get the hang of it. However, I keep getting the error that a nil value is being found while trying to force unwrap a variable. I understand that using forced unwrap isn't safe practice, so I tried to replace it with if let statements, but those just make my calculation result become 0, i.e it never enters the if let statement. The logic of my code is in the picture, any help would be really appreciated.
import SwiftUI
struct ContentView: View {
@State private var firstnumber = 0
@State private var secondnumber = 0
@State private var operand = ""
@State private var calctext = "0"
@State private var isTyping = false
var body: some View {
ZStack { Color.black
.edgesIgnoringSafeArea(.all)
VStack(){
TextField("0", text: $calctext)
.padding(EdgeInsets(top: 150, leading: 20, bottom: 20, trailing: 20))
.border(Color.gray, width: 2)
.multilineTextAlignment(.trailing)
.foregroundColor(Color.white)
.font(.largeTitle)
HStack{
createbutton("AC")
.buttonStyle(numberdesign1())
Spacer()
createbutton("+/-")
.buttonStyle(numberdesign1())
Spacer()
createbutton(" % ")
.buttonStyle(numberdesign1())
Spacer()
Button(action: {
self.operandclick("÷")
}) {
Text(" ÷ ")
}
.buttonStyle(buttondesign())
.cornerRadius(15.0)
}.padding()
HStack{
createbutton(" 9 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 8 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 7 ")
.buttonStyle(numberdesign())
Spacer()
Button(action: {
self.operandclick("×")
}) {
Text(" × ")
}
.buttonStyle(buttondesign())
.cornerRadius(15.0)
}.padding()
HStack{
createbutton(" 6 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 5 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 4 ")
.buttonStyle(numberdesign())
Spacer()
Button(action: {
self.operandclick("-")
}) {
Text(" - ")
}
.buttonStyle(buttondesign())
.cornerRadius(15.0)
}.padding()
HStack{
createbutton(" 3 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 2 ")
.buttonStyle(numberdesign())
Spacer()
createbutton(" 1 ")
.buttonStyle(numberdesign())
Spacer()
Button(action: {
self.operandclick("+")
}) {
Text(" + ")
}
.buttonStyle(buttondesign())
.cornerRadius(15.0)
}.padding()
HStack{
Button(action: {
self.digitTapped("0")
}) {
Text(" 0 ")
}
.foregroundColor(Color.white)
.padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
.background(Color.gray)
.border(Color.gray, width: 1)
.cornerRadius(40.0)
Spacer()
createbutton(" . ")
.buttonStyle(numberdesign())
Spacer()
Button(action: {
self.calculate()
}) {
Text(" = ")
}
.buttonStyle(buttondesign())
.cornerRadius(15.0)
}.padding()
}
.aspectRatio(contentMode: .fill)
}
}
private func createbutton(_ number: String) -> Button<Text> {
return Button(action: {
self.digitTapped(number)
}) {
(Text(number))
}
}
private func digitTapped(_ number: String) -> Void {
if isTyping {
calctext += number
} else {
calctext = number
isTyping = true
}
}
private func operandclick(_ operand: String) {
isTyping = false
// if Int(calctext) != nil {
firstnumber = Int(calctext)!
// }
self.operand = operand
calctext = operand
}
private func calculate() {
isTyping = false
var result = 0
// if Int(calctext) != nil {
secondnumber = Int(calctext)!
// }
if operand == "+" {
result = firstnumber + secondnumber
print (result, firstnumber, secondnumber)
} else if operand == "-" {
result = firstnumber - secondnumber
} else if operand == "÷" {
result = firstnumber / secondnumber
} else if operand == "×" {
result = firstnumber * secondnumber
}
calctext = "\(result)"
}
}
struct buttondesign: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(Color.white)
.padding()
.background(LinearGradient(gradient: Gradient(colors: [Color("DarkGreen"),Color("LightGreen")]), startPoint: .leading, endPoint: .trailing))
.clipShape(Circle())
}
}
struct numberdesign: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(Color.white)
.padding(.all)
.background(Color.gray)
.border(Color.gray, width: 1)
.clipShape(Circle().scale(1))
}
}
struct numberdesign1: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(Color.black)
.padding(.all)
.background(Color("LightGray"))
.border(Color.gray, width: 1)
.clipShape(Circle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}