1

I wanted to have an empty array of "View" in SwiftUI and fill it later.

When I write it like this:

 @State var pages: [View]

XCode yells at me with this:

Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements

1 Answer 1

1

I would highly not recommend to do that, but if you do like then there are two possible options

  1. either declare as of view type erasure
@State var pages = [AnyView]()
  1. or your specific concrete page type, let's assume it is PageView
@State var pages = [PageView]()
Sign up to request clarification or add additional context in comments.

4 Comments

for option1 I get this error when I want to fill the pages variable: Cannot convert value of type 'YourView.page1.Type' to expected element type 'Array<AnyView>.ArrayLiteralElement' (aka 'AnyView')
Option2 is not possible too, I want to use the exact View thing, a complete page for that
Aha ! I should cast my final view to AnyView!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.