-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

0

1 Answer 1

-1

I found the following solution that works:

init(counter:Binding<Int>, strings:Binding<[String]> = Binding.constant([String]())) {
    self._counter = counter
    self._strings = strings
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.