-1

How can I have an optional argument of type Binding<[String]> in the init function?

@Binding var counter:Int
@Binding var strings:[String]


init(counter:Binding<Int>, strings:Binding<[String]>? ) {
    self._counter = counter
    self._strings = strings ?? Binding.constant([String]())
}


// creating a new view as follows throws an error
MyView(counter: $counter)

The error I get is:

Missing argument for parameter 'strings' in call

simibac
  • 7,672
  • 3
  • 36
  • 48

1 Answers1

-1

I found the following solution that works:

init(counter:Binding<Int>, strings:Binding<[String]> = Binding.constant([String]())) {
    self._counter = counter
    self._strings = strings
}
simibac
  • 7,672
  • 3
  • 36
  • 48