1

我有两个 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 数组中。任何见解将不胜感激。

4

2 回答 2

2

您正在尝试从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
于 2021-03-04T22:33:22.087 回答
0

您正在创建 CarLotData 两次。正确的方法是确保您访问正确的参考。

另一种方法是使 CarLotData 成为单例。但这只有在您将在多个视图中使用这些数据时才是正确的方法。

class CarLotData: ObservableObject {
  static public let shared = CarLotData()
  private init() { }
}

然后你会像这样使用它。确保永远不要调用 CarLotData()

Text("Internet: \(CarLotData.shared.internet)")
于 2021-03-04T22:43:29.857 回答