Reputation: 3
I've been trying to create a menu item for my application in swift, and by pressing different buttons on the menu I want to run different python scripts. I'm very new to Swift, and I'm guessing there's a simple mechanic I'm just missing here.
The problem is now that I'm trying to create a button, with an action being run when it is pressed. The function call is in a Toolbar, which is in itself in a NavigationView:
ToolbarItem {
Menu {
Button("Label", action: myFunc("My string"))
} label: {
Label("Scripts", systemImage: "applescript")
}
}
Where the function myFunc is like this:
func myFunc(_ param: String) -> Void {
print(param)
}
(The integer is just a placeholder for a later value which would correspond to the path of the script I'm trying to run)
The error is:
Cannot convert value of type 'Void' to expected argument type '() -> Void'
The weird thing is that when I exclude the argument in the function call, like calling a:
func myFunc() {
print("Hello")
}
From the same location, it works just fine. Is there a limitation to the buttons like not being able to send arguments to their functions?
I've looked at different solutions, but they all seem way too complicated for this error. I saw one guide where it was because of an empty view, but this doesn't seem to be the error at hand.
The amount of buttons will later be in a ForEach. I'm however a bit conflicted regarding what the best way of storing the paths for the scripts is. I've been thinking about doing an enum with the different paths as rawValues. But I'd like different labels for the buttons than the actual paths of the files, so I'd need two values stored in the rawValues, is this possible? Like using a dictionary and using the name as key and the value as the path. Or do you have any inputs on a better way to solve this problem?
I'm very open for suggestions, the goal is just to be able to run scripts with buttons in a menu!
Upvotes: 0
Views: 127
Reputation: 2446
The action
parameter of a Button
view expects an argument of type () -> Void
. This means that you need to pass a reference to a function/closure that takes no arguments and returns nothing.
That's why, when you removed the argument from myFunc
(resulting in this button code:
Button("Label", action: myFunc)
it worked, because it's not calling myFunc
, but simply referencing it. If you want to call myFunc
with an argument, you can wrap it in a closure, like so:
Button("Label", action: { myFunc("My string") })
In this way, the closure takes no arguments and returns nothing, matching the required type.
Upvotes: 1
Reputation: 54706
The problem is that you are executing the function myFunc
by putting the parentheses after it, which will result in a Void
value rather than a function of type () -> Void
, which the Button
initialiser expects.
You can fix this by wrapping your function call in a closure.
ToolbarItem {
Menu {
Button("Label", action: { myFunc("My string") })
} label: {
Label("Scripts", systemImage: "applescript")
}
}
This way you really are passing a closure of type () -> Void
to the Button
init (which it expects), which will only be executed on the button tap. With your previous implementation, the function was executed as soon as the Button
is initialised rather than when it it tapped.
Upvotes: 1