【问题标题】:SwiftUI: How could I stuff assertions in viewbuilders?SwiftUI:如何在视图构建器中填充断言?
【发布时间】:2022-12-14 02:45:21
【问题描述】:
        AsyncImage(url: url) { phase in
            switch phase {
            case .empty:
                ProgressView()
            case .success(let image):
                imageProcessed(image: image)
            case .failure:
                Image(systemName: "photo")
            @unknown default: assertionFailure()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this yields Type '()' cannot conform to 'View'
                // Since the AsyncImagePhase enum isn't frozen,
                // we need to add this currently unused fallback
                // to handle any new cases that might be added
                // in the future:
                EmptyView()
     ^^^^^^^^^^^^^^^^^ adding return here yields Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder'
            }
        }

【问题讨论】:

    标签: swiftui viewbuilder


    【解决方案1】:

    View body 中有一个常用于 print 的技巧,您可以使用:

    let _ = assertionFailure()
    

    通过像这样进行“假”变量赋值,结果生成器不会抱怨语法错误。

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2013-08-20
      相关资源
      最近更新 更多