【问题标题】:SwiftUI ListStyle - Result values in '? :' have mismatching typesSwiftUI ListStyle - '? :' 类型不匹配
【发布时间】:2020-12-12 16:24:45
【问题描述】:

我正在尝试对 iOS 和 macOS 使用相同的视图,仅更改 .listStyle(),因为 InsetGroupedListStyle() 在 macOS 上不可用。

@ViewBuilder
    var body: some View {
        if subject.tasks.count == 0 {
            
            VStack {
                Text("app.main.smartList.noTasks")
                Image(systemName: "tray.fill").font(.system(size: 90)).padding(40)
            }.navigationTitle("Empty")
            
        } else {
            
                List(subject.tasks, id: \.id) { task in
                    TaskView(task: task).environmentObject(self.controller)
                }
                .listStyle(controller.currentOS == OS.iOS ? InsetGroupedListStyle() : DefaultListStyle())
                .navigationTitle(subject.name)
        }
    }

但我一直收到此错误。

'? 中的结果值:' espressione 有不匹配的类型 'InsetGroupedListStyle' 和 'DefaultListStyle'

他们不是都列出样式吗?

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    它们都是列表样式,但具体类型不同,所以 swift 类型检查器不允许这样,我们可以使用自定义修饰符,例如

    extension List {
        @ViewBuilder
        func insetListStyle(if flag: Bool) -> some View {
            if flag {
                self.listStyle(InsetGroupedListStyle())
            } else {
                self     // implicit DefaultListStyle
            }
        }
    }
    

    现在使用它

    List(subject.tasks, id: \.id) { task in
        TaskView(task: task).environmentObject(self.controller)
    }
    .insetListStyle(if: controller.currentOS == OS.iOS)
    

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多