How can I pass Content into a function that will return a swiftUI Container view?

I'm attempting to create a function that will return a swiftUI container. Something like this:

Code Block
    func myStack(content: Text) -> some View {
        ZStack{
            if self.shouldBeHorizontal() {
                HStack(alignment: .center, spacing: 10, content: {content})
            } else {
                VStack(alignment: .center, spacing: 10, content: {content})
            }
        }
    }


However, I really want toe content input to be more generic.
When I change the function declaration to
Code Block
    func myStack(content: ZStack) -> some View {

The compiler seems to require me to specify the details of the content of the ZStack.
Alternatively, when I try this:
Code Block
    func myStack(content: () -> Content) -> some View {

It complains that Content is an undeclared type.

any/all help greatly appreciated.
thanks,
Mike
Answered by OOPer in 612236022
You can make your method explicitly generic.

Please try something like this:
Code Block
    func myStack<Content: View>(content: Content) -> some View {
        ZStack{
            if self.shouldBeHorizontal() {
                HStack(alignment: .center, spacing: 10, content: {content})
            } else {
                VStack(alignment: .center, spacing: 10, content: {content})
            }
        }
    }


or this:
Code Block
    func myStack<Content: View>(@ViewBuilder content: ()->Content) -> some View {
        ZStack{
            if self.shouldBeHorizontal() {
                HStack(alignment: .center, spacing: 10, content: content)
            } else {
                VStack(alignment: .center, spacing: 10, content: content)
            }
        }
    }


Try to use AnyView() this way:
Code Block swift
struct ContentView: View {
  var body: some View {
    self.myStack(content: AnyView(Text("hello")))
  }
  func myStack(content: AnyView) -> some View {
    ZStack{
      if false {
        HStack(alignment: .center, spacing: 0) {
          content
        }
      } else {
        VStack(alignment: .center, spacing: 0) {
          content
        }
      }
    }
  }
}


Accepted Answer
You can make your method explicitly generic.

Please try something like this:
Code Block
    func myStack<Content: View>(content: Content) -> some View {
        ZStack{
            if self.shouldBeHorizontal() {
                HStack(alignment: .center, spacing: 10, content: {content})
            } else {
                VStack(alignment: .center, spacing: 10, content: {content})
            }
        }
    }


or this:
Code Block
    func myStack<Content: View>(@ViewBuilder content: ()->Content) -> some View {
        ZStack{
            if self.shouldBeHorizontal() {
                HStack(alignment: .center, spacing: 10, content: content)
            } else {
                VStack(alignment: .center, spacing: 10, content: content)
            }
        }
    }


Thanks OOPer, your reply was exactly what I was looking for.
Any chance you can refer me to Swift Syntax documentation explaining in general what <Content: View> is?
(Seems new dev forums has lost the functionality to make replies put on another reply...)

Any chance you can refer me to Swift Syntax documentation explaining in general what <Content: View> is?


It is described in the section Type Constraints of Generics in the Swift book.

How can I pass Content into a function that will return a swiftUI Container view?
 
 
Q