我有两个 ObservableObject 类:CarLotData 和 StopWatchManager。我想要做的是使用我的 StopWatchManager 类来加载 CarLotData 属性 timeStep——这是一个数组 Double 类型。
这是我的 StopWatchManager 代码:
import Foundation
import SwiftUI
class StopWatchManager: ObservableObject {
@ObservedObject var carLotData = CarLotData()
var timer = Timer()
@Published var mode: stopWatchMode = .stopped
@Published var secondsElapsed = 0.0
enum stopWatchMode {
case running
case stopped
case paused
}
func start() {
mode = .running
timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
self.secondsElapsed = self.secondsElapsed + 0.1
}
}
func stop() {
timer.invalidate()
if carLotData.timeStep.count < 7 {
carLotData.timeStep.append(secondsElapsed)
print(carLotData.timeStep)
mode = .stopped
}
}
}
这是我的 CarLotData 代码:
import SwiftUI
import Foundation
class CarLotData: ObservableObject {
@Published var carLotData: Int = 0
@Published var carStep: [Double] = []
var id = UUID()
var newUsed: Int = 0
var powerType: Int = 0
var downPayment: Int = 0
var salesmanType: Int = 0
var appointment: Int = 0
var internet: Int = 0
var retail: Int = 0
var carType: Int = 0
var carBrand: Int = 0
var purchaseType: Int = 0
var creditRank: Int = 0
var warranty: Int = 0
var service: Int = 0
var thirdParty: Int = 0
var timeStep: [Double] = []
var salesman: Int = 0
}
最后,这是我的观点,我得到“timeStep = []”,我想要一个 Double 值数组。
import SwiftUI
struct CarLotDataView: View {
@ObservedObject var carLotData = CarLotData()
@ObservedObject var stopWachManager = StopWatchManager()
var body: some View {
List {
Group {
Text("Customer ID: \(carLotData.id)")
Text("Salesman: \(carLotData.salesman)")
Text("New/Used: \(carLotData.newUsed)")
Text("Power Type: \(carLotData.powerType)")
Text("Down Payment: \(carLotData.downPayment)")
Text("Salesman Type: \(carLotData.salesmanType)")
Text("Appointment: \(carLotData.appointment)")
}
Group {
Text("Internet: \(carLotData.internet)")
Text("Retail: \(carLotData.retail)")
Text("Car Type: \(carLotData.carType)")
Text("Car Brand: \(carLotData.carBrand)")
Text("Purchase Type: \(carLotData.purchaseType)")
Text("Credit Rankd: \(carLotData.creditRank)")
Text("Warranty: \(carLotData.warranty)")
Text("Service: \(carLotData.service)")
Text("Third Party: \(carLotData.thirdParty)")
Text("Time Steps: \(carLotData.timeStep)" as String)
}
}
}
}
其他一切正常,只是我的计时器的值没有转移到我的 timeStep 数组中。任何见解将不胜感激。