My question is quite general but still one which may help others in the community. I am coding a function in an app in which a user sets a weekly limit of how much they wish to spend every day, and the app totals their expenditure each day and tells them whether or not they exceeded their limit for the week. As you may be able to tell, this requires an If statement to determine what the app will tell the user depending on whether or not the totaled expenditure exceeded their limit. However, no matter where I put my If statement, there will always be one error or another, and I just don't understand how to include this If statement without error. All I'm asking is that someone checks my code and tells me how I can change the If statement or the code itself to allow them to co-exist. Thank you!
import SwiftUI
struct ProgressChart: View {
@State private var WeekNumber = 1
@State private var SetLimit = ""
@State private var MondaySum = ""
@State private var TuesdaySum = ""
@State private var WednesdaySum = ""
@State private var ThursdaySum = ""
@State private var FridaySum = ""
@State private var SaturdaySum = ""
@State private var SundaySum = ""
@State private var Result = ""
var Total: Double {
let MondaIsANumber = Double(MondaySum) ?? 0
let TuesdayIsANumber = Double(TuesdaySum) ?? 0
let WednesdayIsANumber = Double(WednesdaySum) ?? 0
let ThursdayIsANumber = Double(ThursdaySum) ?? 0
let FridayIsANumber = Double(FridaySum) ?? 0
let SaturdayIsANumber = Double(SaturdaySum) ?? 0
let SundayIsANumber = Double(SundaySum) ?? 0
let SetLimitNumber = Double(SetLimit) ?? 0
let amount = Double(MondaIsANumber + TuesdayIsANumber + WednesdayIsANumber + ThursdayIsANumber + FridayIsANumber + SaturdayIsANumber + SundayIsANumber) ?? 0
if self.Total > SetLimitNumber {
Result = "Unfortunately, you have surpassed your set limit this week."
} else if self.Total < SetLimitNumber {
Result = "Good job! You managed to stay under this week's limit!"
}
return amount
}
var body: some View {
VStack {
Text("Week:\(WeekNumber)")
.font(.largeTitle)
.fontWeight(.bold)
.multilineTextAlignment(.leading)
.padding(.top)
Form {
Section{
Text("What is my budget on products found online this week? ¥ \(SetLimit)" )
.bold()
TextField("¥", text: $SetLimit)
.keyboardType(.decimalPad)
}
Text("Daily sum of money spent on goods seen online")
Section(header: Text("The Working Week")){
Text("Monday: \(MondaySum)" )
TextField("¥", text: $MondaySum)
.keyboardType(.decimalPad)
Text("Tuesday: \(TuesdaySum)" )
TextField("¥", text: $TuesdaySum)
.keyboardType(.decimalPad)
Text("Wednesday: \(WednesdaySum)" )
TextField("¥", text: $WednesdaySum)
.keyboardType(.decimalPad)
Text("Thursday: \(ThursdaySum)" )
TextField("¥", text: $ThursdaySum)
.keyboardType(.decimalPad)
Text("Friday: \(FridaySum)" )
TextField("¥", text: $FridaySum)
.keyboardType(.decimalPad)
}
Section(header: Text("The Weekend")) {
Text("Saturday: \(SaturdaySum)" )
TextField("¥", text: $SaturdaySum)
.keyboardType(.decimalPad)
Text("Sunday: \(SundaySum)" )
TextField("¥", text: $SundaySum)
.keyboardType(.decimalPad)
}
Section {
Text("This Week's Total: $\(Total)")
.bold()
.font(.title)
Text("\(Result)")
}
}
.navigationTitle("My Progress Chart")
}
}
struct ProgressChart_Previews: PreviewProvider {
static var previews: some View {
ProgressChart()
}
}
}
Aucun commentaire:
Enregistrer un commentaire