-2

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
burnsi
  • 6,194
  • 13
  • 17
  • 27
Tirna
  • 383
  • 1
  • 12
  • You are not using reportView in any way. Thats what the "warning" is all about. In your case its probably best to just delete it. – burnsi Jul 01 '22 at 17:56
  • and where is that tab view? – Asperi Jul 01 '22 at 17:57
  • @burnsi How do I inject the environment object into a view then? FYI let reportView = ReportView().environmentObject(questionsAsked) should be let homeView = HomeView().environmentObject(questionsAsked). That is equivalent to the ContentView that appears in every new project. – Tirna Jul 01 '22 at 18:39

1 Answers1

0

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.

burnsi
  • 6,194
  • 13
  • 17
  • 27