使用.OnReceive方法时无法更新SwiftUI预览

时间:2020-10-08 06:22:20

标签: ios swift canvas swiftui combine

我已经在SwiftUI视图中实现了.OnReceive方法,以便能够跟踪某些变量。

onCreate

states

但是正如您所看到的,我已经注释了所有.onReceive的代码,原因是如果不这样做,我会在画布上收到以下错误:

canvas error

这是我的ContentView_Previews结构: enter image description here

有人可以帮助我理解该错误吗?我怀疑ContentView_Previews Struct需要一些额外的值才能解释画布上的视图。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

此错误表示预览无法渲染,其原因是预览@EnvironmentObject var appState: States中没有LoginView使用的.onReceive

因此,您需要进行设置:

struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
      LoginView()
         .previewLayout(.device)
         .environmentObject(States())  // here
   }
}