0

I have a view called TagView which creates a list of tags using LazyHGrid I am trying to get the array of tags that loads from the web service and pass it from the main view to TagView. Here is the code:

struct TagsView: View {

var tags: Array<String>
private var layout = [GridItem(.fixed(30))]

var body: some View {
    HStack {
        Spacer()
        GeometryReader { geo in
            ScrollView(.horizontal, showsIndicators: false) {
            
                    LazyHGrid(rows: layout) {
                        ForEach(tags, id: \.self) {
                            Button("\($0)") {
                                
                            }
                            .font(.callout.bold())

                        }
                    }
                }
           }
    

After creating the variable: var tags: Array<String> I need to get the array from the main view like this:

struct MainView: View {
    var model: Model
    var body: some View {
        VStack {
            TagsView(tags: model.tags)
        }
    }
}

But I am getting this error:

TagsView' initializer is inaccessible due to 'private' protection level

I tried with @Binding and still no luck, any help would be great!

2
  • show the code of how you pass model into your MainView. Commented Aug 25, 2022 at 9:02
  • 1
    @workingdogsupportUkraine the networking part is not yet done but assume something like this: @State private var array = [String]() init() { array = model.tags } Commented Aug 25, 2022 at 9:07

3 Answers 3

1

The reason for this error is that the synthesized init is being marked private since one of the properties is private. To solve the problem, remove the private or add:

init(tags: Array<String>) {
    self.tags = tags
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just change layout to a let constant:

    private let layout = [GridItem(.fixed(30))]

Then the auto-generated init will not be private.

Comments

1

Using Generate memberwise initializer fixed the problem:

internal init(tags: Array<String>) {
        self.tags = tags
    }

enter image description here

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.