[ad_1]
I’m making an app with numerous screens, and I need to acquire all of the screens that each one conform to View in an array of views. On the app’s essential display, I need to present a listing of all of the display titles with a NavigationLink to all of them.
The issue I’m having is that if I attempt to create a customized view struct and have a property that initialises to a given display and returns it. I preserve operating into points and the compiler forces me to alter the variable to simply accept any View moderately than simply View or the erased sort some View.
struct Display screen: View, Identifiable {
var id: String {
return title
}
let title: String
let vacation spot: any View
var physique: some View {
NavigationLink(vacation spot: vacation spot) { // Kind 'any View' can't conform to 'View'
Textual content(title)
}
}
}
This works:
struct Display screen<Content material: View>: View, Identifiable {
var id: String {
return title
}
let title: String
let vacation spot: Content material
var physique: some View {
NavigationLink(vacation spot: vacation spot) {
Textual content(title)
}
}
}
However the subject with this method is that I can’t put totally different screens into an Array which may be fastened as follows:
struct AllScreens {
var screens: [any View] = []
init(){
let testScreen = Display screen(title: "Charties", vacation spot: SwiftChartsScreen())
let testScreen2 = Display screen(title: "Take a look at", vacation spot: NavStackScreen())
screens = [testScreen, testScreen2]
}
}
However when I attempt to entry the screens in a Checklist it can’t infer what view it’s with out typecasting. The top end result I’m attempting to realize is with the ability to go in an array of screens and get their title displayed in a listing like under.

For the time being I can solely obtain this by hardcoding the lists components, which works.
import SwiftUI
struct MainScreen: View {
let screens = AppScreens.allCases
let allScreens = AllScreens()
var physique: some View {
NavigationStack() {
Checklist() {
AppScreens.chartsScreen
AppScreens.navStackScreen
}
.navigationTitle("WWDC 22")
}
}
}
[ad_2]
