Home > Software design >  Cannot convert value of type 'Void' to expected argument type '(String) -> Void�
Cannot convert value of type 'Void' to expected argument type '(String) -> Void�

Time:04-20

I'm writing an application when this error appeared to me: Cannot convert value of type 'Void' to expected argument type '(String) -> Void'

The function is:

func updateSymbolsPadding(symbolType: String, symbolSize:inout Double) -> Void {
        var size: Double
        if symbolType=="multiply" {
            size = 2.4
        } else if symbolType=="circle" {
            size = 2.0
        } else if symbolType=="triangle" {
            size = 2.0
        } else if symbolType=="square" {
            size = 2.0
        } else if symbolType=="pentagon" {
            size = 2.0
        } else if symbolType=="octagon" {
            size = 2.0
        }
        symbolSize = size
    }

And I called it back in a picker:

Picker("symbol one", selection: $symbolOne, content: {
                        Image(systemName: "multiply")
                            .tag("multiply")
                        Image(systemName: "circle")
                            .tag("circle")
                        Image(systemName: "triangle")
                            .tag("triangle")
                        Image(systemName: "square")
                            .tag("square")
                        Image(systemName: "pentagon")
                            .tag("pentagon")
                        Image(systemName: "octagon")
                            .tag("octagon")
                    })
                    .onChange(of: symbolOne, perform: updateSymbolsPadding(symbolType: symbolOne, symbolSize: &symbolOneSize))

variables are:

@Binding var symbolOne: String
@Binding var symbolOneSize: Double

How can I solve the problem?

CodePudding user response:

Like Ptit Xav said: The perform parameter must be a closure not the result of a function call.

Replace

            .onChange(of: symbolOne, perform: updateSymbolsPadding(symbolType: symbolOne, symbolSize: &symbolOneSize))

With

    .onChange(of: symbolOne) { symbolOne in
        updateSymbolsPadding(symbolType: symbolOne, symbolSize: &symbolOneSize)
    }
  • Related