skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. The closure that HStack (and many other views) accept is a kind of function builder called ViewBuilder. It only supports up to 10 Views as arguments. These are all hardcoded in the SwiftUI module:

    static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View, C9 : View
    

    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:

    HStack {
        ForEach(0..<11) { i in
            BarView(barGradeValue: i, barSendValue: i < 6 ? (i + 1) * 10 : (11 - i) * 10, barFlashValue: 10)
        }
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search