为@environmentObject设置SceneDelegate时的编译器警告

发布于 2025-02-12 05:49:42 字数 1588 浏览 0 评论 0原文

我正在尝试实现@environmentObject以将数组从一个选项卡中的视图传递到另一个选项卡中的另一个视图。

我收到黄色编译器警告:

从未使用了不可变价价值的初始化'reportview'; 考虑替换为“ _”或删除

删除

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var questionsAsked = QuestionsAsked()

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

        ProductsStore.shared.initializeProducts()

        if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(ViewRouter()))
          self.window = window
          window.makeKeyAndVisible()
        }

        let reportView = ReportView().environmentObject(questionsAsked)
    }
}

import Foundation

class QuestionsAsked: ObservableObject {
    @Published var sectionThings = [SectionThing]()
    @Published var memoryPalaceThings = [MemoryPalaceThing]()
}

SceneDelegate

@EnvironmentObject var questionsAsked: QuestionsAsked

我将数据传递到这样的数据:

questionsAsked.sectionThings = testSectionThings ?? []

在将数据传递给我的位置:

@EnvironmentObject var questionsAsked: QuestionsAsked

然后我如下:

totalThings += questionsAsked.sectionThings.count
totalThings += questionsAsked.memoryPalaceThings.count

I'm trying to implement @EnvironmentObject to pass an array from a View in one tab to another view in another tab.

I get the yellow compiler warning:

Initialization of immutable value 'reportView' was never used;
consider replacing with assignment to '_' or removing it

in SceneDelegate.swift

Here is my SceneDelegate.swift:

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var questionsAsked = QuestionsAsked()

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

        ProductsStore.shared.initializeProducts()

        if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(ViewRouter()))
          self.window = window
          window.makeKeyAndVisible()
        }

        let reportView = ReportView().environmentObject(questionsAsked)
    }
}

Here is my ObservabelObject:

import Foundation

class QuestionsAsked: ObservableObject {
    @Published var sectionThings = [SectionThing]()
    @Published var memoryPalaceThings = [MemoryPalaceThing]()
}

I use:

@EnvironmentObject var questionsAsked: QuestionsAsked

in my view that generates the data to be passed around.

I pass in the data like so:

questionsAsked.sectionThings = testSectionThings ?? []

In the view where the data is to be passed to I have:

@EnvironmentObject var questionsAsked: QuestionsAsked

I then access it as follows:

totalThings += questionsAsked.sectionThings.count
totalThings += questionsAsked.memoryPalaceThings.count

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-02-19 05:49:42

这里的问题是,该行无用,因为它不会在现场中呈现。唯一提出的视图是MotherView。为了将问题传递给向下视图,您只需像其他.environmentObject那样附加它。因此,这应该读取:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    // this really need to be @StateObject´s
    @StateObject var viewRouter = ViewRouter()
    @StateObject var questionsAsked = QuestionsAsked()

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

        ProductsStore.shared.initializeProducts()

        if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
            //Inject them here into the MotherView
            window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(viewRouter).environmentObject(questionsAsked))
          self.window = window
          window.makeKeyAndVisible()
        }
    }
}

MotherView和降序视图中,您可以通过@environmentObject包装访问它们。

The issue here is that the line in question is useless as it will not be presented in the Scene. The only view that is presented is MotherView. In order to pass questionsAsked down to that view you just append it like the other .environmentObject. So this should read:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    // this really need to be @StateObject´s
    @StateObject var viewRouter = ViewRouter()
    @StateObject var questionsAsked = QuestionsAsked()

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

        ProductsStore.shared.initializeProducts()

        if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
            //Inject them here into the MotherView
            window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(viewRouter).environmentObject(questionsAsked))
          self.window = window
          window.makeKeyAndVisible()
        }
    }
}

In the MotherView and descending Views you can now access them by the @EnvironmentObject wrapper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文