TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Compiler warning when setting up SceneDelegate for @EnvironmentObject

Forums > SwiftUI

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

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.