Swiftui 我的列表不显示在模拟器中,但在预览中显示良好

Swiftui 我的列表不显示在模拟器中,但在预览中显示良好,swiftui,Swiftui,我用SwiftUI创建了一个列表。我的列表在预览中显示良好,但在模拟器中没有显示: struct ContentView: View { var sandwiches: [Sandwich] = [] var body: some View { NavigationView { List(sandwiches) { item in NavigationLink(destination: Image(item.

我用SwiftUI创建了一个列表。我的列表在预览中显示良好,但在模拟器中没有显示:

struct ContentView: View {
    var sandwiches: [Sandwich] = []
    var body: some View {
        NavigationView {
            List(sandwiches) { item in
                NavigationLink(destination: Image(item.name)) {
                    Image(item.name)
                        .resizable()
                        .frame(width: 40, height: 40)
                    VStack(alignment: .leading) {
                        Text(item.name)
                        Text("En Savoir plus...")
                            .font(.subheadline)
                            .foregroundColor(.secondary)
                    }
                }
            }.navigationBarTitle("Sandwiches")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(sandwiches: testData)
    }
}


Xcode 11.5版(11E608c)

在预览中,您可以创建
内容视图,如下所示:

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)
它将
testData
分配给
ContentView
中的
sandwiches
属性

但是,在真实设备/模拟器上运行时,
ContentView
通常在
SceneDelegate
中创建

SceneDelegate
中的
ContentView
可能是这样创建的:

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)
它不会给您任何错误,因为您为
sandwiches
变量提供了一个默认参数(并且编译器允许您跳过它):

如果像这样创建
ContentView

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)

在预览中,您可以按如下方式创建
ContentView

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)
它将
testData
分配给
ContentView
中的
sandwiches
属性

但是,在真实设备/模拟器上运行时,
ContentView
通常在
SceneDelegate
中创建

SceneDelegate
中的
ContentView
可能是这样创建的:

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)
它不会给您任何错误,因为您为
sandwiches
变量提供了一个默认参数(并且编译器允许您跳过它):

如果像这样创建
ContentView

ContentView(sandwiches: testData)
ContentView()
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView() // <- here you need to pass `sandwiches`

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    ...
}
ContentView(sandwiches: testData)