SwiftUI 致命错误:找不到类型为“”的 ObservableObject 和 @EnvironmentObject 错误:可能作为此视图的祖先丢失

pla*_*pus 8 ios swift swiftui

有关目标的详细信息:我想从 firebase 中获取数据,以问答的格式在行中列出数据。答案将有一个文本字段来更新它。有一个保存按钮可以通过将问题和答案上传到 firebase 来保存答案。

期望与结果: 当点击以导航到屏幕时,我的主菜单中应该有一个按钮。结果是我的应用程序冻结,并且在控制台中出现以下错误。

错误

Fatal error: No ObservableObject of type AllQuestionsAndAnswersClass found.
A View.environmentObject(_:) for AllQuestionsAndAnswersClass may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros/Monoceros-39.4.4/Core/EnvironmentObject.swift, line 55
2020-01-24 13:21:25.523326-0600 FirebaseStarterApp[5025:894100] Fatal error: No ObservableObject of type AllQuestionsAndAnswersClass found.
A View.environmentObject(_:) for AllQuestionsAndAnswersClass may be missing as an ancestor of this view.: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/Monoceros/Monoceros-39.4.4/Core/EnvironmentObject.swift, line 55
Run Code Online (Sandbox Code Playgroud)

我的想法 我不太确定从哪里开始,但我认为这与必须将环境对象添加到预览并在 App Delegate 中执行某些操作有关。我看过一些关于 Scene Delegate 的东西,但我只有 App Delegate,我不确定是否有区别

我尝试过的 我尝试查看以下链接,但它们对我的案例没有帮助SwiftUI @EnvironmentObject 错误:可能作为此视图的祖先丢失

找不到类型的 ObservableObject

这是我的SwiftUI 屏幕代码

@available(iOS 13.0, *)
final class AllJobDataClass: ObservableObject  {
    @Published var allJobDataArray = [JobInfo]()
}

@available(iOS 13.0, *)
final class AllQuestionsAndAnswersClass: ObservableObject  {
    @Published var questionsAndAnswersInfoArray = [QuestionAndAnswer]()
}

@available(iOS 13.0.0, *)
struct SingleQuestionAndAnswerRow: View {

     @State var singleQuestionAndAnswerVar: QuestionAndAnswer

     var body: some View {

       return HStack{
             Text("\(singleQuestionAndAnswerVar.question)")

             TextField("Enter some text", text: $singleQuestionAndAnswerVar.answer)
                 .border(Color.black)
         }
     }
}


@available(iOS 13.0.0, *)
struct NewQuestionsScreenSwiftUIView: View {

    @EnvironmentObject var allJobDataVar: AllJobDataClass
    @EnvironmentObject var allQuestionsAndAnswerVar: AllQuestionsAndAnswersClass

    init()
    {
        fetchDataFromFirebase()
    }

    var body: some View {
        ScreenBackground
        {
            List(self.allQuestionsAndAnswerVar.questionsAndAnswersInfoArray) { questionAndAnswer in
                    //here is where questionAnswerRow is presented
                SingleQuestionAndAnswerRow(singleQuestionAndAnswerVar: questionAndAnswer)

                }
        }
        .onTapGesture {
            self.endEditingFunction()
        }
    }
}

@available(iOS 13.0.0, *)
struct NewQuestionsScreenSwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        NewQuestionsScreenSwiftUIView()
    }
}

Run Code Online (Sandbox Code Playgroud)

我的App Delegate代码

import UIKit
import Firebase
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Window setup
        FirebaseApp.configure()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UINavigationController(rootViewController: ATCClassicLandingScreenViewController(nibName: "ATCClassicLandingScreenViewController", bundle: nil))
        window?.makeKeyAndVisible()

        return true
    }
  }
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 8

您将环境对象声明为

struct NewQuestionsScreenSwiftUIView: View {
    @EnvironmentObject var allJobDataVar: AllJobDataClass
    @EnvironmentObject var allQuestionsAndAnswerVar: AllQuestionsAndAnswersClass
Run Code Online (Sandbox Code Playgroud)

但是您必须在NewQuestionsScreenSwiftUIView使用之前将它们注入到某个地方,例如在创建时(或通过父视图隐式地)。在提供的代码中,它在您创建的位置不可见NewQuestionsScreenSwiftUIView(),但在那个地方它应该类似于

NewQuestionsScreenSwiftUIView()
    .environmentObject(AllJobDataClass()) // < or instance if created before
    .environmentObject(AllQuestionsAndAnswersClass()) // < or instance if created before
Run Code Online (Sandbox Code Playgroud)