Swift 我的ObserveObject类数组变量';时间步长';无法保存值
我有两个ObservieObject类:CarLotData和StopWatchManager。我试图做的是使用StopWatchManager类加载CarLotData属性timeStep——这是一种双数组类型 以下是我的秒表管理器代码:Swift 我的ObserveObject类数组变量';时间步长';无法保存值,swift,swiftui,observableobject,observedobject,Swift,Swiftui,Observableobject,Observedobject,我有两个ObservieObject类:CarLotData和StopWatchManager。我试图做的是使用StopWatchManager类加载CarLotData属性timeStep——这是一种双数组类型 以下是我的秒表管理器代码: import Foundation import SwiftUI class StopWatchManager: ObservableObject { @ObservedObject var carLotData = CarLotData()
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
}
}
}
最后,这里是我的视图,在这里我得到“timeStep=[]”,我想要一个双值数组
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数组。如果您有任何见解,我们将不胜感激。您正试图从秒表管理器中访问和更新CarLotData,您正在这样做,但您在视图中使用的是另一个,您必须使用您更新的同一个 诀窍总是使用与开始时相同的引用强> 将此行添加到您的CarLotData:
static let shared: CarLotData = CarLotData()
let carLotData = CarLotData.shared
然后在秒表管理器上使用此代码:
static let shared: CarLotData = CarLotData()
let carLotData = CarLotData.shared
不要在类内使用@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()
值得一提的是,
秒表管理器
是一个可观察对象
,而不是视图
。因此,在那里使用@ObservedObject
没有任何意义。我不想重构代码,只想让代码按照操作码工作,谢谢。完全按照预期工作。您提到使用@StateObject var carLotData=carLotData.shared“down code”。引用不清楚,而且我根本没有使用StateObject。如果你能详细介绍一下,我将不胜感激。再次感谢。正如我所说,我会的,但你可以使用你喜欢的,我的理由是,因为视图将是你的应用程序和模型的主界面,我想使用StateObject而不是ObservedObject,苹果说,在我们可以使用StateObject的地方,它比ObservedObject更好,ObservedObject以疯狂的方式控制和读取数据源,我想说的是比StateObject要多得多,因此StateObject更好。也许还有其他事情,但我告诉了你重要的事情。谢谢,你的反馈非常有用。