I am trying to create an extension on Button that reduces the boilerplate when creating them with a system image.
extension Button {
init(_ systemName: String, action: @escaping () -> Void) {
self.init {
action()
} label: {
Image(systemName: systemName) // <-- Error
}
}
}
This results in the following compiler error:
Cannot convert value of type 'Image' to closure result type 'Label'
Looking at Button's initializer, I see it declared like this:
struct Button<Label> : View where Label : View {
init(action: @escaping () -> Void, @ViewBuilder label: () -> Label) { ... }
}
But specifying my extension similarly also doesn't work. Where am I going wrong?
extension Button where Label : View {