多态性和 SwiftUI

fuz*_*uzz 6 polymorphism inheritance casting swift swiftui

给出以下示例:

class ProfileTab: Identifiable {
    let id = UUID()
    let name: String

    init(name: String) {
        self.name = name
    }
}

class ProfileQuoteTab: ProfileTab {
    let answer: String
    let prompt: String

    init(name: String, answer: String, prompt: String) {
        self.answer = answer
        self.prompt = prompt
        super.init(name: name)
    }
}

class ProfileImageTab: ProfileTab {
    let image: Image

    init(name: String, image: Image) {
        self.image = image
        super.init(name: name)
    }
}

struct ContentView: View {
    let tabs: [ProfileTab] = [
        ProfileQuoteTab(name: "This is a quote tab", answer: "This is an answer", prompt: "This is a prompt"),
        ProfileImageTab(name: "This is an image tab", image: Image(systemName: "faceid"))
    ]

    var body: some View {
        List(self.tabs) { tab in
            VStack {
                if tab is ProfileQuoteTab {
                    Text((tab as! ProfileQuoteTab).answer)
                }

                if tab is ProfileImageTab {
                    (tab as! ProfileImageTab).image
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有更好/推荐的方法来访问ProfileTab数组中不同类的属性?

我知道这样做会导致:

包含控制流语句的闭包不能与函数生成器“ViewBuilder”一起使用

if let profileImage = tab as? ProfileImage {
    ...
}
Run Code Online (Sandbox Code Playgroud)

除了执行以下操作之外,还有更干净的方法吗?

(tab as! ProfileImageTab).image

Asp*_*eri 2

在所描述的用例中,多态性将如下所示(或某些变体):

class ProfileTab: Identifiable {
    let id = UUID()
    let name: String

    init(name: String) {
        self.name = name
    }

    func view() -> AnyView {
        AnyView(EmptyView())
    }
}

class ProfileQuoteTab: ProfileTab {
    let answer: String
    let prompt: String

    init(name: String, answer: String, prompt: String) {
        self.answer = answer
        self.prompt = prompt
        super.init(name: name)
    }

    override func view() -> AnyView {
        AnyView(Text(self.answer))
    }
}

class ProfileImageTab: ProfileTab {
    let image: Image

    init(name: String, image: Image) {
        self.image = image
        super.init(name: name)
    }

    override func view() -> AnyView {
        AnyView(self.image)
    }
}

struct ContentView: View {
    let tabs: [ProfileTab] = [
        ProfileQuoteTab(name: "This is a quote tab", answer: "This is an answer", prompt: "This is a prompt"),
        ProfileImageTab(name: "This is an image tab", image: Image(systemName: "faceid"))
    ]

    var body: some View {
        List(self.tabs) { tab in
            VStack {
                tab.view()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)