The code below is in my var body: some View {
. When I run this, the app builds and runs exactly how I want. The problem is, if I remove one of the commented rows I get "Extra argument in call", and if I remove the comment for both I get the error "Extra arguments at positions #11, #12 in call". I really only need to call it 11 times, I only tried the 12 to see if the error was limited to 10 or just the last value.
Why would this work calling BarView() 10 times but not 11 or 12? I have tried to put this into a 1…10 loop instead but also could not figure out how to get that to work.
I am using xCode 12.2. I have been able to find a lot of people with the same error I get, but none of them are for the same reason so I have been unable to diagnose on my own. I really only build apps for fun and often stumble through the process and I’m just really stuck on this so any help would be really appreciated. Thanks.
HStack{
BarView(barGradeValue: 0, barSendValue: 10, barFlashValue: 10)
BarView(barGradeValue: 1, barSendValue: 20, barFlashValue: 10)
BarView(barGradeValue: 2, barSendValue: 30, barFlashValue: 10)
BarView(barGradeValue: 3, barSendValue: 40, barFlashValue: 10)
BarView(barGradeValue: 4, barSendValue: 50, barFlashValue: 10)
BarView(barGradeValue: 5, barSendValue: 60, barFlashValue: 10)
BarView(barGradeValue: 6, barSendValue: 50, barFlashValue: 10)
BarView(barGradeValue: 7, barSendValue: 40, barFlashValue: 10)
BarView(barGradeValue: 8, barSendValue: 30, barFlashValue: 10)
BarView(barGradeValue: 9, barSendValue: 20, barFlashValue: 10)
//BarView(barGradeValue: 10, barSendValue: 10, barFlashValue: 10)
//BarView(barGradeValue: 10, barSendValue: 10, barFlashValue: 10)
}
Then to show what this is calling, BarView() is the code below. Again, it works just how I want when I call it 10 times, just not more than that.
struct BarView: View {
var barGradeValue: Int
var barSendValue: CGFloat
var barFlashValue: CGFloat
//reference to main struct above
var content = ContentView()
var body: some View {
VStack {
Text(content.gradesV[barGradeValue])
ZStack (alignment: .top){
//Full height
Capsule().frame(width: 20, height: 100)
.foregroundColor(Color.gray)
//Flash Value
Capsule().frame(width: 20, height: barSendValue+barFlashValue)
.foregroundColor(Color.yellow)
//Send Value
Capsule().frame(width: 20, height: barSendValue)
.foregroundColor(content.gradesColor[barGradeValue])
}
}
}
}
2
Answers
The @ViewBuilder system in SwiftUI is limited to 10 views within any given view container. There’s no argument for an 11th view, so you get that error.
The solution is to use Group or separate the views
The closure that
HStack
(and many other views) accept is a kind of function builder calledViewBuilder
. It only supports up to 10View
s as arguments. These are all hardcoded in the SwiftUI module:The designers could hardcode more, but decided not to. This is probably because you can simplify your view’s code to not need to put that many views in a view builder.
In this case, you can use a
ForEach
: