我的 ObservableObject Class 数组变量 'timeStep' 无法保存值
My ObservableObject Class Array Variable 'timeStep' Fails To Hold Values
我有两个 ObservableObject class:CarLotData 和 StopWatchManager。我想做的是使用我的 StopWatchManager class 加载 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 数组。任何见解将不胜感激。
您正在尝试从 StopWatchManager 访问和更新 CarLotData 并且您正在这样做,但是您在视图中完全使用了另一个,你必须使用你更新的同一个!
诀窍是始终使用与您开始时相同的参考!
将此行添加到您的 CarLotData:
static let shared: CarLotData = CarLotData()
然后在 StopWatchManager 上使用此代码:
let carLotData = CarLotData.shared
不要在Class里面使用@ObservedObject,只要用let,就可以了。
最后在您的视图中使用它:
@ObservedObject var carLotData = CarLotData.shared
对于我的个人应用程序,我会使用以下代码:
@StateObject var carLotData = CarLotData.shared
您正在创建 CarLotData 两次。正确的方法是确保您访问正确的引用。
另一种方法是将 CarLotData 设为单例。但只有在多个视图中使用此数据时,这才是正确的方法。
class CarLotData: ObservableObject {
static public let shared = CarLotData()
private init() { }
}
那你就这么用吧。确保永远不要调用 CarLotData()
Text("Internet: \(CarLotData.shared.internet)")
我有两个 ObservableObject class:CarLotData 和 StopWatchManager。我想做的是使用我的 StopWatchManager class 加载 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 数组。任何见解将不胜感激。
您正在尝试从 StopWatchManager 访问和更新 CarLotData 并且您正在这样做,但是您在视图中完全使用了另一个,你必须使用你更新的同一个!
诀窍是始终使用与您开始时相同的参考!
将此行添加到您的 CarLotData:
static let shared: CarLotData = CarLotData()
然后在 StopWatchManager 上使用此代码:
let carLotData = CarLotData.shared
不要在Class里面使用@ObservedObject,只要用let,就可以了。
最后在您的视图中使用它:
@ObservedObject var carLotData = CarLotData.shared
对于我的个人应用程序,我会使用以下代码:
@StateObject var carLotData = CarLotData.shared
您正在创建 CarLotData 两次。正确的方法是确保您访问正确的引用。
另一种方法是将 CarLotData 设为单例。但只有在多个视图中使用此数据时,这才是正确的方法。
class CarLotData: ObservableObject {
static public let shared = CarLotData()
private init() { }
}
那你就这么用吧。确保永远不要调用 CarLotData()
Text("Internet: \(CarLotData.shared.internet)")