Skip to main content
added 99 characters in body
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157

Recommended: use my ViewExtractor package as it's much more robust than the code here.

Recommended: use my ViewExtractor package as it's much more robust than the code here.

Added warning at top for when not to use this.
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157

It's rare that you need to extract views from an array. If you are just looking to pass @ViewBuilder content into a view, you can simply do the following:

struct ContentView: View {
    var body: some View {
        VStackReplica {
            Text("1st")
            Text("2nd")
            Text("3rd")
        }
    }
}

struct VStackReplica<Content: View>: View {
    @ViewBuilder let content: () -> Content

    var body: some View {
        VStack(content: content)
    }
}

If this isn't sufficient for your use-case, then see below.


I have got a generic version working, so there is no need to make multiple initializers for different lengths of tuples. In addition, the views can be anything you want (you are not restricted for every View to be the same type).

I have got a generic version working, so there is no need to make multiple initializers for different lengths of tuples. In addition, the views can be anything you want (you are not restricted for every View to be the same type).

It's rare that you need to extract views from an array. If you are just looking to pass @ViewBuilder content into a view, you can simply do the following:

struct ContentView: View {
    var body: some View {
        VStackReplica {
            Text("1st")
            Text("2nd")
            Text("3rd")
        }
    }
}

struct VStackReplica<Content: View>: View {
    @ViewBuilder let content: () -> Content

    var body: some View {
        VStack(content: content)
    }
}

If this isn't sufficient for your use-case, then see below.


I have got a generic version working, so there is no need to make multiple initializers for different lengths of tuples. In addition, the views can be anything you want (you are not restricted for every View to be the same type).

added 111 characters in body
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157

You can find a Swift Package I made for this at GeorgeElsham/ViewExtractor. TheThat contains more than what's in this answer, because this answer is just a simplified & basic version. Since the code is slightly different to this answer, so read the README.md first for an example.

You can find a Swift Package I made for this at GeorgeElsham/ViewExtractor. The code is slightly different to this answer, so read the README.md first for an example.

You can find a Swift Package I made for this at GeorgeElsham/ViewExtractor. That contains more than what's in this answer, because this answer is just a simplified & basic version. Since the code is slightly different to this answer, so read the README.md first for an example.

Bounty Awarded with 50 reputation awarded by CommunityBot
Added GitHub link for Swift Package.
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157
Loading
deleted 96 characters in body
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157
Loading
Source Link
George
  • 30.9k
  • 12
  • 100
  • 157
Loading