SwiftUI:无法推断引用成员的上下文基础

SwiftUI: Cannot infer contextual base in reference to member

我为 SwiftUI 字体编写了一个扩展,它引入了一些自定义字体。

extension Font {
    static let solDisplay: Font = .custom("Gilroy", size: 36)
    static let solHeadline: Font = .custom("Gilroy", size: 24)
}

现在由于某种原因在构建过程中出现以下错误:Cannot infer contextual base in reference to member 'heavy'。 有趣的是,这个错误是其他人在执行合并请求时发现的——我在本地没有注意到任何这些错误,但它似乎只发生在一些人身上。目前我们团队中有四分之二的人犯了其他人没有的错误。

struct AssetsDemoPage: View {
    let fonts: [(String, Font)] = [
        ("Display - Heavy", .solDisplay.weight(.heavy)),   // Error Happening here
        ("Display - Light", .solDisplay),                  // No error here!
        ("Headline - Heavy", .solHeadline.weight(.heavy)), // Error Happening here
        ("Headline - Light", .solHeadline),                // No error here!
    ]

    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Fonts")) {
                    ForEach(fonts, id: \.0) { name, font in
                        Text(name)
                            .font(font)
                    }
                }
            }
            .navigationBarTitle(Text("Assets"), displayMode: .inline)
        }
    }
}

由于@jnpdx 指的是 Xcode 版本,因此 Swift 版本可能不同。在 Swift 5.4 中,实现了 SE-0287

该提案允许隐式成员链。您可以阅读有关链接的提案的更多信息,但这很好地总结了它:

This proposal suggests the expansion of implicit member syntax to more complex expressions than just a single static member or function. Specifically, implicit member syntax would be allowed to cover chains of member references.

Color.red.opacity(0.5) 等代码现在可以简化为 .red.opacity(0.5),类似于您的示例。

Xcode 12.5 需要使用 Swift 5.4,所以让团队知道升级。