How To Make Optional Variables in SwiftUI View

Viewed 31

I checked out some posts related to this but they were either primitive type variables or View.

Please see sample code

struct Test: View {
    var mystruct: MyStruct?
    
    init() { }
    
    var body: some View {
        Text(String(description: (mystruct == nil))
    }

}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test(MyStruct())
    }
}

In Preview, i provided an instance of MyStruct(). But this code returns an arrow. I do not want to provide an object in the view because it will fetch data from a url. I only want the preview to be populated with a MyStruct object so i can see the visual preview of the ui.

The suggested post provided is not relevant here since it required to have a value provided. Or is this not possible in SwiftUI?

0 Answers
Related