SwiftUI ListStyle - '?:' 类型不匹配 [英] SwiftUI ListStyle - Result values in '? :' have mismatching types

查看:30
本文介绍了SwiftUI ListStyle - '?:' 类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the same view both for iOS and macOS changing only .listStyle() since InsetGroupedListStyle() is not available on 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)
        }
    }

但我一直收到这个错误.

But I keep receiveing this error.

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

它们不都是列表样式吗?

Aren't they both List Styles?

推荐答案

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

They are both list styles, but of different concrete types, so swift type checker does not allow such, instead we can use custom modifier like

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)

这篇关于SwiftUI ListStyle - '?:' 类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆