-1

I am new to Swift/SwiftUI and I am running into an issue. I have a VStack with several item in it, e.g. Text(“1“), Text(“2“), Text(“3“)… Text(“13“) and I am getting the error Extra arguments at positions #11, #12, #13 in call. If I use just with few items, it works well.

The code looks like this

import SwiftUI

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

Thanks, Santhosh

Santhosh
  • 1
  • 2

1 Answers1

1

The maximum number of items you can have on the same level (in this case within the VStack()) is 10. In your specific example it would be Text(“1“) to Text(“10“). If you need more, you can use groups. Now you can have 10 groups on the same level and each group can contain 10 items on its own. E.g.:

VStack() {

    // Group #1
    Group() {
        Text(“1“)
        Text(“2“)
        Text(“3“)
        Text(“4“)
        Text(“5“)
        Text(“6“)
        Text(“7“)
        Text(“8“)
        Text(“9“)
        Text(“10“)
    }

    // Group #2
    Group() {
        Text(“11“)
        Text(“12“)
        Text(“13“)
        Text(“14“)
        Text(“15“)
        Text(“16“)
        Text(“17“)
        Text(“18“)
        Text(“19“)
        Text(“20“)
    }
}

So you are able to create 100 items (10*10). If you need more, you even can nest groups into groups.

E.g.:

VStack() {

    // Outer Group #1
    Group() {

        // Inner Group #1_1
        Group() {
            Text(“1“)
            ...
            Text(“10“)
        }

        // Inner Group #1_2
        Group() {
            Text(“11“)
            ...
            Text(“20“)
        }
    }


    // Outer Group #2
    Group() {

        // Inner Group #2_1
        Group() {
            Text(“101“)
            ...
            Text(“110“)
        }

        // Inner Group #2_2
        Group() {
            Text(“111“)
            ...
            Text(“120“)
        }
    }
}

Btw. If you want to create lists like that, you also can use a ForEach loop (instead of VStack() you also can use a List() element):

VStack() {
    ForEach(1..<14) { i in
        Text("\(i)“)
    }
}

Or if you use an array, you can iterate over the array:

VStack() {
    ForEach(values, id: \.self) { value in
        Text(„\(value)“)
    }
}

Here we go with a full example:

//
//  ContentView.swift
//  SwiftTest
//
//  Created by Sebastian on 10.08.22.
//

import SwiftUI

struct ContentView: View {
    
    let values: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    
    var body: some View {
        HStack {
            
            VStack() {
                Text("Groups")
                // Outer group
                Group() {
                    Text("1")
                    Text("2")
                    Text("3")
                    // Inner group #1
                    Group() {
                        Text("4")
                        Text("5")
                        Text("6")
                        Text("7")
                        Text("8")
                        Text("9")
                        Text("10")
                        Text("11")
                        Text("12")
                        Text("13")
                    }
                    // Inner group #2
                    Group() {
                        Text("14")
                        Text("15")
                        Text("16")
                        Text("17")
                        Text("18")
                        Text("19")
                        Text("20")
                        Text("21")
                        Text("22")
                        Text("23")
                    }
                }
            }
            
            VStack() {
                Text("ForEach with a defined range")
                ForEach(0..<14) { i in
                    Text("\(i)")
                }
            }
            
            VStack() {
                Text("ForEach with an pre-defined array")
                ForEach(values, id: \.self) { value in
                    Text("\(value)")
                }
            }
        }
    }
}

Best, Sebastian

Sebastian Fox
  • 1,324
  • 1
  • 10
  • 20