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) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
// Use a UIHostingController as window root view controller.
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