Quantcast
Channel: User trojanfoe - Stack Overflow
Viewing all articles
Browse latest Browse all 226

Answer by trojanfoe for How to create a view that can display multiple objects?

$
0
0

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")))

enter image description here

With:

let viewController = NSHostingController(rootView: CardView(item: Item2(n: "Name2", d: "Description2")))

enter image description here


Viewing all articles
Browse latest Browse all 226

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>