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

标签 swift swiftui swift5

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

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' .
有趣的是,此错误是其他人在执行 Pull Request 时发现的 - 我在本地没有注意到这些错误中的任何一个,但它似乎只发生在一些人身上。目前,我们团队中四分之二的人有其他人没有的错误。
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) ,类似于您的示例。
使用 Swift 5.4 需要 Xcode 12.5,所以让团队知道升级。

关于SwiftUI:无法推断引用成员的上下文基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67494326/

相关文章:

swift - 枚举关联值

arrays - 与 Unsplash 合作。无法将类型 'Array<_>' 的值分配给类型 'Any?'

ios - 在 SwiftUI 中按下按钮时如何检测并采取措施

swift - Swift如何从Set removeFirst中捕获错误?

ipad - 当多个连接的场景在前台处于事件状态时获取当前聚焦的 UIScene

ios - 在 Swift 中重新加载 UIScrollView 中的 UITableView

ios - 在 SWIFT 中的不同 View 之间切换

SwiftUI 内容在较小的 iPhone 屏幕上被截断

swiftui - 如何在 SwiftUI 中手动显示上下文菜单?

swift - Swift 5 更新后 Firebase Cocoa Pods 错误