0

I have always read that swiftUI has a 10 subview limit. The code on this github page seems to add 50 views without grouping. I cannot figure out how or why this works. It is very similar to what I want to achieve which is to create some bard charts.

https://github.com/Matt54/SwiftUI-AudioKit-Visualizer/blob/master/Visualizer/Views/AmplitudeVisualizer.swift

james
  • 2,595
  • 9
  • 43
  • 70
  • 1
    Please paste the referenced code directly in question. – pawello2222 Jan 26 '21 at 23:28
  • You don't need to "always read" it. You can see how many placeholder types the overloads have been supplied for, [in the docs](https://developer.apple.com/documentation/swiftui/viewbuilder). (`C0` through `C9` is what you're looking for.) Until variadic generics come to be, we'll be limited by those overloads. But there's no guarantee that we won't get more; 10 is practical but arbitrary. –  Jan 26 '21 at 23:42
  • But what is so important that made you raise this question? I am not getting! We can solve the issue with using Group{} maybe in some upcoming new Version of Xcode, could we able omit Group as well for content more than 10, but I do not care if I use Group or not, it is not a big deal for me, why this is a big deal for you? – ios coder Jan 27 '21 at 00:07

1 Answers1

3

From the @ViewBuilder perspective, a ForEach view is treated as one view.

It means that if you write:

HStack {
    ForEach(0..<11, id: \.self) {
        Text("\($0)")
    }
}

you only put one view in the HStack even if as the result you create 11 views.

The problem occurs when you try to put 11 views directly:

HStack {
    Text("1")
    Text("2")
    Text("3")
    Text("4")
    Text("5")
    Text("6")
    Text("7")
    Text("8")
    Text("9")
    Text("10")
    Text("11") // error - now it's the 11-th view
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209