1

I have a simple CardView container that gets a view in its initializer.

struct CardView<Content: View>: View {
    @ViewBuilder var content: Content
    
    var body: some View {
        content
            .background(Color.green)
            .cornerRadius(20)
    }
}

When I use it in a view body it works ✅:

struct ScreenView: View {
    var body: some View {
        CardView {
            Text("Hello!")
                .padding()
        }
    }
}

enter image description here

but when I try to keep a reference to the view I get an error:

let card = CardView {
    Text("Hello!")
        .padding()
}

enter image description here

Tal Cohen
  • 1,308
  • 10
  • 20
  • 2
    You shouldn’t be trying to store references to views. It’s an antipatterm in SwiftUI. They are transient and may change from render to render. – jnpdx Nov 21 '21 at 07:06

0 Answers0