简体   繁体   中英

Cannot convert value of type 'Void' to expected argument type '(String) -> Void'

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM