How To Make Optional Variables in SwiftUI View

Please see sample code

struct Test: View {
    var array: [String] = []
    
    init() { }
    
    var body: some View {
    Text(String(description: array.count)
    }

}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test(array: ["1", "2", "3"])
    }
}

This one is a string array, I wish to pass some values only in preview because in the main view, i will be fetching data from a url. While setting this variable array to an empty array is the solution, what if my variable is a Struct? or Class? How will i make this optional and only supply a new Struct/Class instance in the preview only

How about something like this...

import SwiftUI

struct Test: View {
    
    let someClass: SomeClass?
    
    var body: some View {
        VStack {
            Text("Test")
            if let someClass {
                Text(someClass.name)
            }
        }
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        let someClass = SomeClass()
        /// set up your preview SomeClass properties here...
        return Test(someClass: someClass)
    }
}


class SomeClass {
    let name = "Fred"
}

And of course you can use:

Test(someClass: nil)

Thank you for helping out. More or less what you provided is what i had tried

The only remaining part to correct is the if let someClass.

It worked when i did if let someClass = someClass

Thanks

How To Make Optional Variables in SwiftUI View
 
 
Q