为@environmentObject设置SceneDelegate时的编译器警告
我正在尝试实现@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,该行无用,因为它不会在现场中呈现。唯一提出的视图是
MotherView
。为了将问题传递给
向下视图,您只需像其他.environmentObject
那样附加它。因此,这应该读取:在
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 passquestionsAsked
down to that view you just append it like the other.environmentObject
. So this should read:In the
MotherView
and descending Views you can now access them by the@EnvironmentObject
wrapper.