I need a funtion like:

func makeVariableRows(start: Int, rows: Int) -> (some View, Int)
{
	let view = VStack(alignment: .leading)
	{
		ForEach( 0 ..< rows )	 
		{ 	row in
			Text("this is row \(start + row)")
		}	 
	}
	return (view, rows)
}

but Xcode tells me: "Non-constant range: argument must be an integer literal"

So I created several funtions, like:

func make2Rows(start: Int) -> (some View, Int) {...}
func make3Rows(start: Int) -> (some View, Int) {...}
...

func makeVariableRows2(start: Int, rows: Int) -> (some View, Int)?
{
	switch rows
	{
		case 2:	return make2Rows(start: start)
		case 3:	return make3Rows(start: start)
		case 4:	return make4Rows(start: start)
		default:	return nil
	}
}

Xcode does not like this either, complaining that "Function declares an opaque return type 'some View', but the return statements in its body do not have matching underlying types"

Next attempt:

let functions = [make2Rows, make3Rows, ...]
for (idx, function) in functions.enumerated()
{
	let (view, nbr) = function(start: idx + 2)
}

Again a failure:

Xcode:
"Cannot convert value of type 'Any' to specified type '(_, _)'"
"Extraneous argument labels 'start:' in call"

Any idea how to solve this?

Gerriet.

This is a SwiftUI question, which should really be asked on Apple Developer Forums.

However, you might try the following first before heading over there.

func makeVariableRows (start: Int, count: Int) -> (some View, Int) {
    struct Row: Identifiable {
        let id: Int
    }

    let rows = (0..<count).map ({Row (id:$0)})
    let view = VStack(alignment: .leading) {
        ForEach (rows) {row in
            Text("this is row \(start + row.id)")
        }
    }
    return (view, count)
}

That one relates on how you use ForEach inside the function, not to the function signature itself. As @ibex10 pointed out, SwiftUI related questions are better to discuss on Apple forums. Or in this case you can reach for the documentation on the ForEach to see how it is intended to be used by design.

That one seems like a new thing to you, so I suggest get familiar with opaque types in Swift: Documentation.

but Xcode tells me: "Non-constant range: argument must be an integer literal"
That one relates on how you use ForEach inside the function, not to the function signature itself. As @ibex10 pointed out, SwiftUI related questions are better to discuss on Apple forums. Or in this case you can reach for the documentation on the ForEach to see how it is intended to be used by design.

I did study the documentation on ForEach, but did not find the very clever trick of @ibex10. This either means that the documentation could be improved or that I am especially stupid.

That one seems like a new thing to you, so I suggest get familiar with opaque types in Swift: Documentation.

I am right now doing this.

Just out of curiosity:

How does one declare and use an array of functions?
On the internet I did find only examples of functions without argument-labels returning Any or Void.

That's all in the first parapraph, actually:

Use ForEach to provide views based on a RandomAccessCollection of some data type. Either the collection’s elements must conform to Identifiable or you need to provide an id parameter to the ForEach initializer.

Of course documentation won't give immediate solutions, it is about understanding how to use it in general. See that you need either have Identifiable or id provided, @ibex10 did use the first one. You could've used id parameter, if you didn't want. That's the power docs give you.

Note that this does not imply your fault, nor documentation one (at least in this case, yet it can be confusing sometimes), but that just some practice will help you get more from the docs in the first take. Believe me, I have struggled reading documentation a lot (and still do sometimes).

In function type Swift doesn't allow labels (don't remember why btw, but probably not to be too restrictive what function you can pass as arg labels part of the signature), so you have to use underscore as primary label, but still provide secondary (you can omit labels at all, I just don't particularly like that variant):

typealias MyFuncType = (_ arg1: Int, _ arg2: String) -> Bool
let myArray: [MyFuncType] = []
1 Like

Expanding a little bit on @vns's example.

// Using array of functions...

@main
enum Temp {
    static func main ()  {
        func bar (u: Int) -> String {
            "\(#function): \(u)"
        }
        
        struct S {
            let dc: Int
            
            static func foo (u: Int) -> String {
                "\(type (of: Self.self)) \(#function): \(u))"
            }
            
            func add (u: Int, v: Int) -> Int {
                u + v + dc
            }
        }
        
        do {
            // Case 1 - Using global and S.Type functions
            typealias Proc = (Int) -> String
            
            let procs: [Proc] = [bar, S.foo(u:)]
            for proc in procs {
                let v = proc (1024)
                print (v, proc)
            }
        }
        
        print ()
        do {
            // Case 2 - Using instance functions of S
            typealias Proc = (S) -> ((Int, Int) -> Int)
            
            let s1 = S (dc: 5)
            let s2 = S (dc: -5)
            
            let procs: [(S, Proc)] = [(s1, S.add), (s2, S.add)]
            for proc in procs {
                let u = 3
                let v = 5
                let r = proc.1 (proc.0) (u, v)
                // := let r = (proc.1 (proc.0)) (u, v)
                print (r, proc.1, proc.1 (proc.0))
            }
        }
        
        print ()
        do {
            // Case 3 - Using partially applied instance functions of S
            typealias Proc = (Int, Int) -> Int
            
            let s1 = S (dc: 5)
            let s2 = S (dc: -5)
            
            let procs: [Proc] = [s1.add, s2.add]
            for proc in procs {
                let u = 3
                let v = 5
                let r = proc (u, v)
                print (r, proc)
            }
        }
        
        print ()
        do {
            // Case 4 - Mix case 1 and case 3
            func add (u: Int, v: Int) -> Int {
                u + v
            }

            typealias Proc = (Int, Int) -> Int
            
            let s1 = S (dc: 5)
            let s2 = S (dc: -5)
            
            let procs: [Proc] = [s1.add, s2.add, add]
            for proc in procs {
                let u = 3
                let v = 5
                let r = proc (u, v)
                print (r, proc)
            }
        }
    }
}

Output:

bar(u:): 1024 (Function)
S.Type foo(u:): 1024) (Function)

13 (Function) (Function)
3 (Function) (Function)

13 (Function)
3 (Function)

13 (Function)
3 (Function)
8 (Function)