在SwiftUI预览中,实例成员不能用于类型上。

问题描述 投票:2回答:1

我的预览中出现以下错误。

struct DetailView: View {
    var header: DataProvider.DataHeader

    var body: some View {
        Text("...")
    }
}

struct DetailView_Previews: PreviewProvider {
    var a = DataProvider.DataHeader(title: "a", text: "b")

    static var previews: some View {
        DetailView(header: a)
    }
}

错误是:

Instance member 'a' cannot be used on type 'DetailView_Previews'

为什么会发生这种情况?

swiftui preview
1个回答
4
投票

这是因为 static var preview,

所以,既要用静态的,也要用

static var a = DataProvider.DataHeader(title: "a", text: "b")

DetailView(header: DataProvider.DataHeader(title: "a", text: "b"))
© www.soinside.com 2019 - 2024. All rights reserved.