SwiftUI Ambiguous reference to member ‘buildBlock()’

So this work:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Text 1")
            Text("Text 2")
            Text("Text 3")
            Text("Text 4")
            Text("Text 5")
            Text("Text 6")
            Text("Text 7")
            Text("Text 8)
            Text("Text 9")
            Text("Text 10")
        }
    }
}

But this doesn’t:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Text 1")
            Text("Text 2")
            Text("Text 3")
            Text("Text 4")
            Text("Text 5")
            Text("Text 6")
            Text("Text 7")
            Text("Text 8)
            Text("Text 9")
            Text("Text 10")
            Text("Text 11")
        }
    }
}

The compiler refuses to compile and prompts the following error: Ambiguous reference to member 'buildBlock()'.

This is due to the view building system in SwiftUI internally having code to let you add 1 view, 2 views, 3 views… up to 10 views, but nothing for 11 and beyond.

The solution is to wrap up to 10 elements in a Group, it allows you to break your components structure, for example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Group {
                Text("Text 1")
                Text("Text 2")
                Text("Text 3")
                Text("Text 4")
                Text("Text 5")
                Text("Text 6")
                Text("Text 7")
                Text("Text 8)
                Text("Text 9")
                Text("Text 10")
            }
            Text("Text 11")
        }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *