在 Canvas SwiftUI 上崩溃

Crash on Canvas SwiftUI

我正在使用新的 iOS 框架 SwiftUI 实现一个小应用程序。我正在使用 @EnvironmentObject 绑定我的数据以供查看。一切正常,但 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