ios - 使用 .OnReceive 方法时无法更新 SwiftUI 预览
问题描述
我已经在 SwiftUI 视图中实现了 .OnReceive 方法,以便能够跟踪一些变量。
但正如您所看到的,我对所有 .onReceive 的代码进行了注释,原因是如果我不这样做,我会在画布上收到以下错误:
谁能帮我理解错误?我怀疑 ContentView_Previews 结构需要一些额外的值才能解释画布上的视图。
谢谢您的帮助!
解决方案
此错误意味着预览无法呈现,其原因是它没有@EnvironmentObject var appState: States
您LoginView
在.onReceive
.
所以,你需要设置它:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
.previewLayout(.device)
.environmentObject(States()) // here
}
}
推荐阅读
- python - 如何在python中制作分隔符
- java - Spring Kafka如何动态创建多个消费者
- tensorflow - 将 K.function 与 K.gradients 一起使用时出错
- github - 如何将 WSO2 与 GitHub 集成?
- javascript - Cookie 可能返回错误的格式
- javascript - Jquery 和 Flask 的问题
- javascript - 后端突然停止工作并抛出此错误“找不到模块'../../cast/boolean'”
- python - Tensorboard 不显示来自估计器的任何标量摘要
- python - 将 random.choice 变成可变的
- android - 那是我吗?还是 android studio 更改了我的整个 build.gradle 代码,现在我的整个项目都搞砸了?