2

我正在使用 DataStoreenvironmentObject在我的应用程序周围传递数据,但是我需要为视图创建特定的 View Model 类,但我需要能够将 my 的实例传递dataStore给 myWorkoutDetailViewModel但可以想不出办法。我尝试将它作为参数传递给WorkoutDetailViewModel,但编译器抱怨Cannot use instance member 'dataStore' within property initializer; property initializers run before 'self' is available。我还能怎么做?

struct NewWorkoutDetailView: View {
    
   @EnvironmentObject var dataStore: DataStore
   
    
   @StateObject var workoutDetailViewModel = WorkoutDetailViewModel(dataStore: dataStore)
    
    
    var body: some View {

    }

 }


final class WorkoutDetailViewModel: ObservableObject {

    var dataStore: DataStore
    
    @Published var fullyLoadedWorkout: TrackerWorkout?
    @Published var notes: String = ""
    @Published var workoutImage = UIImage()
    @Published var workoutLocation: String = ""
    
    
    public func editWorkout() {
       dataStore.loadWorkouts() 
    }

 }
4

0 回答 0