SwiftUI: Cannot infer contextual base in reference to member

11,203

As @jnpdx is referring to with the Xcode versions, the Swift version may not be the same. In Swift 5.4, SE-0287 was implemented.

This proposal allowed for implicit member chains. You can read more on the proposal linked, but this sums it up pretty well:

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.

Code such as Color.red.opacity(0.5) can now be simplified to .red.opacity(0.5), similar to in your example.

Xcode 12.5 is required to use Swift 5.4, so let the team know to upgrade.

Share:
11,203
JaWeilBaum
Author by

JaWeilBaum

Updated on June 25, 2022

Comments

  • JaWeilBaum
    JaWeilBaum about 2 years

    I wrote an extension for SwiftUI Font which introduces a couple of custom fonts.

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

    Now for some reason there is the following error appears during the build process: Cannot infer contextual base in reference to member 'heavy'. Interestingly this error was found by someone else while doing a Pull Request - I did not notice any of these errors locally, but it appears to happen to only some. Currently 2 out of 4 people in our team have the errors where the other ones don't.

    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)
            }
        }
    }