How To Declare Optional Parameter Function

I have a function like this

@ViewBuilder
  func addInfo(_ text: String, action: (() -> Void)?, addDivider: Bool = true, centerAlignment: Bool = false, isBold: Bool = false) -> some View {
    VStack {
      Text(text)
        .fontWeight(isBold ? .bold : .regular)
        .frame(maxWidth: .infinity, alignment: centerAlignment ? .center : .leading)
        .padding([.leading, .trailing], 10)
        .padding([.top, .bottom], 5)        
        .contentShape(Rectangle())
        .onTapGesture {
          if let action {
            action()
          }
        }
      if addDivider {
        Divider()
      }
    }
  }

I do not understand why i get a compile error

Expected type after '->'

if i call the function like this

addInfo("Sample", action: () -> Void {
           
})

The error points to Void. What is the correct way to do this? Please advise.

Answered by chitgoks in 740626022

Baaah.. managed to solve it

addInfo("Sample", action: () {
           
})
Accepted Answer

Baaah.. managed to solve it

addInfo("Sample", action: () {
           
})

Sure, when you call a func, you must not repeat the return type.

How To Declare Optional Parameter Function
 
 
Q