Nabend zusammen,
irgendwie komme ich einfach nicht weiter.
Ich habe ein paar Propertys als lazy gesetzt, weil sie beim Start noch nicht zur Verfügung stehen.
Wie bekomme ich es nun hin, dass diese auch aktualisiert werden, wenn sich Werte ändern.
Ich vermute mal, dass ich den falschen Weg eingeschlagen habe, doch ich weiß nicht wo.
Das sollte im beste Fall funktionieren:
1. Anhand des aktuellen Datums und dem Zieldatum die Differenz in Tagen, Wochen und Monaten berechnen.
2. Von der Anzahl, welche als Ziel deklariert werden, soll die aktuelle Anzahl abgezogen werden.
3. Das ganze soll dann noch ausgerechnet werden wie viele dies pro Tag, pro Woche, pro Monat sind.
Alles anzeigen
Alles anzeigen
Alles anzeigen
irgendwie komme ich einfach nicht weiter.
Ich habe ein paar Propertys als lazy gesetzt, weil sie beim Start noch nicht zur Verfügung stehen.
Wie bekomme ich es nun hin, dass diese auch aktualisiert werden, wenn sich Werte ändern.
Ich vermute mal, dass ich den falschen Weg eingeschlagen habe, doch ich weiß nicht wo.
Das sollte im beste Fall funktionieren:
1. Anhand des aktuellen Datums und dem Zieldatum die Differenz in Tagen, Wochen und Monaten berechnen.
2. Von der Anzahl, welche als Ziel deklariert werden, soll die aktuelle Anzahl abgezogen werden.
3. Das ganze soll dann noch ausgerechnet werden wie viele dies pro Tag, pro Woche, pro Monat sind.
Quellcode: StatistikView.swift
- struct StatistikView: View {
- @ObservedObject var statistik = Statistik()
- var body: some View {
- VStack{
- VStack{
- Text("Zieldatum: ")
- TextField("31.12.20222", text: $statistik.comparisonDateString)
- }
- VStack {
- Text("Aktuelles Ziel: ")
- TextField("", value: $statistik.goalValue, format: .number)
- .keyboardType(.decimalPad)
- }
- VStack{
- Text("Derzeitiger Stand: ")
- TextField("", value: $statistik.currentValue, format: .number)
- .keyboardType(.decimalPad)
- }
- VStack {
- Text("Pro Tag: \(statistik.perDays)" )
- Text("Pro Woche: \(statistik.perWeeks)")
- Text("Pro Monat: \(statistik.perMonths)")
- }
- }
- }
- }
- struct StatistikView_Previews: PreviewProvider {
- static var previews: some View {
- StatistikView()
- }
- }
Quellcode: Statistik.swift
- import Foundation
- class Statistik : ObservableObject {
- var comparisonDateString = "31.12.2022"
- var goalValue = 3811
- var currentValue = 689
- let dateCalculator = DateCalculator()
- lazy var remainingDays = dateCalculator.calculatesDateDifference(today: Date(), comparisonDateString: comparisonDateString, dateComponent: .day) + 1
- lazy var remainingWeeks = remainingDays / 7
- lazy var remainingMonths = remainingDays / 30
- lazy var perDays = (goalValue - currentValue) / remainingDays
- lazy var perWeeks = (goalValue - currentValue) / remainingWeeks
- lazy var perMonths = (goalValue - currentValue) / remainingMonths
- }
Quellcode: DateCalculator.swift
- import Foundation
- class DateCalculator{
- func calculatesDateDifference(today: Date, comparisonDateString : String, dateComponent : Calendar.Component) -> Int{
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "dd.MM.yyyy"
- let compareDate = dateFormatter.date(from: comparisonDateString)
- let difference = Calendar.current.dateComponents([dateComponent], from: today, to: compareDate!)
- return difference.day!
- }
- }