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

Return from initializer without initializing all stored properties

Forums > Swift

Hello, I am having trouble with fixing this error. What am i doing wrong here and how can i fix it ?

struct GrubbView: View { @StateObject var ViewModel = GrubbViewViewModel() @FirestoreQuery var items: [GrubbItem]

private let userId: String

init(userId: String) {
    self.userId = userId

  }          ** Return from initializer without initializing all stored properties**

var body: some View {
    NavigationView{
        VStack{

        }
        .navigationTitle("Home (Grubb View)")
        .toolbar{
            Button {
                // Action
                ViewModel.showingNewItemView = true
            } label: {
                Image(systemName: "plus")
            }
        }
        .sheet(isPresented: $ViewModel.showingNewItemView) {
            NewItemView(NewItemShown: $ViewModel.showingNewItemView)
        }
    }
}

}

3      

In your code you haven't given items an initial value. That is why you get the error about returning from init without initializing all stored properties.

The fix is to give items an initial value, either in the init or when you declare the property. The following code makes items an empty array initially.

@FirestoreQuery var items: [GrubbItem] = []

4      

As @SwiftDevJournal say or add

init(userId: String) {
  self.userId = userId
  self.items = []
}

4      

@alan has doubled down....

I am having trouble with fixing this error. What am i doing wrong here and how can i fix it ?

It's easy to fix!

Check your message before you post! Make sure all your code is IN BETWEEN the three backtick marks.

``` <-- These are backtick marks.
// Place ALL your code between these marks.
// Your Swift code goes here.
// See?! Easy fix.
```

3      

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!

Reply to this topic…

You need to create an account or log in to reply.

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.