How about this?
protocol ItemProtocol { func name() -> String func description() -> String func optionalThing() -> String?}struct Item1: ItemProtocol { var n: String var d: String var t: String func name() -> String { n } func description() -> String { d } func optionalThing() -> String? { t }}struct Item2: ItemProtocol { var n: String var d: String func name() -> String { n } func description() -> String { d } func optionalThing() -> String? { nil }}struct CardView: View { let item: ItemProtocol var body: some View { VStack(alignment: .center) { Text("\(item.name())") .font(.headline) .accessibilityAddTraits(.isHeader) Spacer() HStack { Label("\(item.description())", systemImage: "questionmark.square.dashed") if let optionalThing = item.optionalThing() { Label("\(optionalThing)", systemImage: "questionmark.square.dashed") } } .font(.caption) } .padding() }}
With:
let viewController = NSHostingController(rootView: CardView(item: Item1(n: "Name1", d: "Description1", t: "Optional Thing")))
With:
let viewController = NSHostingController(rootView: CardView(item: Item2(n: "Name2", d: "Description2")))