1

So I'm doing some refactoring and I ran across this line of code that I wanted to refactor:

struct MyView: View {
     @State private var myArrayOfCustomObjects = [CustomObject]
     let text: String
          var body: some View {
             Text(text)
          }
}

Then when I wanted to refactor the view as so..

struct ExtractedView: View {
  @Binding var customObjects: [CustomObject]
  let text: String

  init(customObjects: Binding<Array<CustomObject>>, text: String) {
    self.customObjects = customObjects  // Error: 'self' used before all stored properties are initialized

        // Also tried _customObjects = customObjects
    self.text = text

  }
     var body: some View {
          
        Text(text)
     }
}

This code is simplified of course but I fear I may be getting that error due to some complexity I'm not exposing in the example. Any feedback is welcome

What am I doing wrong??

( I also have an Environment instance (managedObjectContext) and a coreData class - which has some logic inside of the init that are being initialized too but didn't think it was relevant for this code example )

1 Answer 1

2

This will work! also try clean your build folder and build your project first.

struct ExtractedView: View {
  @Binding var customObjects: [CustomObject]
  let text: String

  init(customObjects: Binding<Array<CustomObject>>, text: String) {
    self._customObjects = customObjects

    self.text = text

  }
     var body: some View {
          
        Text(text)
     }
}


struct CustomObject { }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.