Search code examples
swiftuiviewbuilder

SwiftUI: Generic parameter 'Value' could not be inferred


Following codes gives an error of "Generic parameter 'Value' could not be inferred".

Appreciate anyone could point out the root cause and let me know how to fix it.

Thanks


import SwiftUI

private let readMe = """
  This case study demonstrates how to enhance an existing SwiftUI component so that it can be driven \
  off of optional and enum state.

  The BottomMenuModifier component in this is file is primarily powered by a simple boolean binding, \
  which means its content cannot be dynamic based off of the source of truth that drives its \
  presentation, and it cannot make mutations to the source of truth.

  However, by leveraging the binding transformations that come with this library we can extend the \
  bottom menu component with additional APIs that allow presentation and dismissal to be powered by \
  optionals and enums.
  """

struct ContentView: View {
  @State var count: Int?
    @State private var showPartialSheet = false

  var body: some View {
    Form {

      Button("Show bottom menu") {
        withAnimation {
          self.count = 0
            self.showPartialSheet = true
        }
      }
    }
    .bottomMenu($showPartialSheet, content: {
        VStack {
            Text("dfd")
        }
    })
    .navigationTitle("Custom components")
  }
}

private struct BottomMenuModifier<BottomMenuContent>: ViewModifier
where BottomMenuContent: View {
  @Binding var isActive: Bool
    let content: BottomMenuContent
    
    init(isActive: Binding<Bool>, @ViewBuilder content: () -> BottomMenuContent) {
        self._isActive = isActive
        self.content = content()
    }

  func body(content: Content) -> some View {
    content.overlay(
      ZStack(alignment: .bottom) {
        if self.isActive {
          Rectangle()
            .fill(Color.black.opacity(0.4))
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .onTapGesture {
              withAnimation {
                self.isActive = false
              }
            }
            .zIndex(1)
            .transition(.opacity)

          self.content
            .padding()
            .background(Color.white)
            .cornerRadius(10)
            .frame(maxWidth: .infinity)
            .padding(24)
            .padding(.bottom)
            .zIndex(2)
            .transition(.move(edge: .bottom))
        }
      }
      .ignoresSafeArea()
    )
  }
}

extension View {


  fileprivate func bottomMenu<Value, Content>(
    _ showPartialSheet: Binding<Bool>,
    @ViewBuilder content: @escaping () -> Content
  ) -> some View
  where Content: View {
    self.modifier(
        BottomMenuModifier(isActive: showPartialSheet, content: content)
    )
  }

}

struct CustomComponents_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}


Solution

  • Your bottomMenu function has an extra generic in its signature. Change it to:

    fileprivate func bottomMenu<Content>(
    

    (Note that Value from your original, which is unused, is removed)