1

I'm a beginner to programming and have just added a custom tab bar to allow navigation throughout the app. However, since I have added the property wrappers to let this happen I get the error:

'app does not conform to protocol 'App' '.

It forces me to add the init() protocol stubs but I cannot seem to get it to work- so I can run by code without the error 'Return from initializer without initializing all stored properties'. I've tried everything but still it won't go away.

I updated Xcode last night if that is relevant and the error has only come on as I started working on my project today.

Here's where the error is coming from

import SwiftUI
import Firebase

@main
struct PhotoApp: App {

    init () {
        FirebaseApp.configure()
    }

    @StateObject var viewRouter: ViewRouter
    
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(viewRouter)
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
mstv2020
  • 143
  • 2
  • 6

1 Answers1

2

You need to initialize viewRouter. The reason it gives you an error is that PhotoApp does not conform to App. This is because in the definition of the App protocol, the init is required:

/// Creates an instance of the app using the body that you define for its
/// content.
///
/// Swift synthesizes a default initializer for structures that don't
/// provide one. You typically rely on the default initializer for
/// your app.
init()

If you do not provide viewRouter a value, the memberwise initializer of the struct is used instead. It would be init(viewRouter: ViewRouter), when we just want init().

Without the init() setting a default value for viewRouter or by doing so directly, you got the error "Return from initializer without initializing all stored properties".

The following will work for you:

import SwiftUI
import Firebase

@main
struct PhotoApp: App {

    init () {
        FirebaseApp.configure()
    }

    @StateObject var viewRouter: ViewRouter = ViewRouter()
    
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(viewRouter)
        }
    }
}

Note: Pass in anything the init of ViewRouter requires, if needed.

George
  • 25,988
  • 10
  • 79
  • 133