Home > other >  Passing a value into a struct with generic type
Passing a value into a struct with generic type

Time:01-20

I have written a struct with a generic type like this:

struct AlphabetGrid<Letter: View>: View {
    var letters: [[Letter]]
    var spacing: CGFloat = 8
    ...

But I'd like to pass in the value of spacing when I instantiate the struct, so I can use it in different situations. Is there a way to do that?

CodePudding user response:

You have specified default value for spacing, but you can use this parameter in constructor explicitly to provide new one, like

AlphabetGrid(letters: [[Text("Demo1"), Text("Demo2")]], spacing: 12)
  •  Tags:  
  • Related