Home > Software design >  How to fix "Cannot call value of non-function type `'Binding<() -> ()>'`&quo
How to fix "Cannot call value of non-function type `'Binding<() -> ()>'`&quo

Time:01-06

I am writing this function:

@ToolbarContentBuilder
func toolBarContent(displayMode: Binding<Bool>, showContents: Binding<Bool>) -> some ToolbarContent {
    ToolbarItem(placement: .navigationBarTrailing) {
        HStack {
            Picker("Display Mode", selection: displayMode) {
                Image(systemName: "rectangle.portrait").tag(true)
                Image(systemName: "rectangle.split.2x1").tag(false)
            }.pickerStyle(.segmented)
        }
    }
    ToolbarItem(placement: .navigationBarTrailing) {
        Button {
            showContents.toggle()
        } label: {
            Image(systemName: "list.bullet.rectangle.fill")
        }
    }
}

On the following line:

showContents.toggle()

I get the error:

Cannot call value of non-function type 'Binding<() -> ()>'
  • What does that mean ?
  • How can I define a function that returns some ToolbarContent and takes a Binding<A> parameter ?

CodePudding user response:

You are trying to call toggle on a Binding<Bool> rather than on a Bool. You need to use wrappedValue to access the underlying value of a Binding.

So you need to replace showContents.toggle() with showContents.wrappedValue.toggle().

  • Related