我正在用新的iOS框架SwiftUI实现一个小应用程序。我正在使用@EnvironmentObject
将数据绑定(bind)到 View 。一切正常,但是Canvas崩溃并没有显示任何内容。为什么?
struct CompetitionsListSwiftUIView : View {
@EnvironmentObject var competitionsViewModel: CompetitionsViewModel
var body: some View {
List(self.competitionsViewModel.competitions.identified(by: \.id)) { competition in
CompetitionCellSwiftUIView(competition: competition)
}
}
}
#if DEBUG
struct CompetitionsListSwiftUIView_Previews : PreviewProvider {
static var previews: some View {
CompetitionsListSwiftUIView()
}
}
#endif
Canvas 的错误消息是这样的:
Error Domain=render service Code=12 "Rendering service was interrupted" UserInfo={NSLocalizedDescription=Rendering service was interrupted}
最佳答案
尝试将环境对象添加到预览中:
#if DEBUG
struct CompetitionsListSwiftUIView_Previews : PreviewProvider {
static var previews: some View {
CompetitionsListSwiftUIView()
.environmentObject(CompetitionsViewModel())
}
}
#endif