Canvas 呈现视图,控制台提供正确的输出,但应用程序中的视图保持为空

Canvas renders View, Console gives right output, but View in app stays empty

在 MacOS 上,我有一个非常简单的 ContentView:

struct ContentView: View {
    
    @ObservedObject var environment: Environment
    
    var body: some View {
        
        if let glyph = environment.glyph {
            print ("Content View \(glyph)")
            return AnyView {
                GlyphView(glyph: glyph)
            }
        } else {
            return AnyView {
                Text("no glyph")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var environment = Environment()
    static var previews: some View {
        ContentView(environment: environment)
    }
}

当我 运行 申请时,控制台给出:

2020-10-14 21:50:42.065365+0200 ShapeSearch[94679:5992139] Metal API Validation Enabled
2020-10-14 21:50:42.093298+0200 ShapeSearch[94679:5992191] flock failed to lock maps file: errno = 35
2020-10-14 21:50:42.093688+0200 ShapeSearch[94679:5992191] flock failed to lock maps file: errno = 35
Content View Glyph "k" - 6 contours

ContentView留空,没有GlyphView,没有Text

但是当我调试 GlyphView 时,我可以在 canvas 上看到非常漂亮的渲染字形:

struct GlyphView_Previews: PreviewProvider {
    
    static var glyph = Environment().glyph!
    static var previews: some View {
        GlyphView(glyph: glyph)
    }
}

environment.glyph 在这两种情况下都取自相同的来源。我确定。

编辑:

这样操作:

struct ContentView: View {
    
    @ObservedObject var environment: Environment
    
    var body: some View {
        GlyphView(glyph: environment.glyph!)
    }
}

哪里可能出错?

不清楚您使用的是哪个 SwiftUI 版本,但以下内容在任何情况下都应该有效

var body: some View {
  Group {  
    if nil != environment.glyph {
        GlyphView(glyph: environment.glyph!)
    } else {
        Text("no glyph")
    }
  }
}