So i’ve a view that takes viewModel containing array of parts offering views to construct physique in ForEach
And it appears like this:
struct MainTabView: View {
@ObservedObject var viewModel: TabViewModel
var physique: some View {
TabView {
ForEach(viewModel.tabs, id: .title) { tab in
tab.view
.tabItem {
Picture(systemName: tab.imageName)
Textual content(tab.title)
}
}
}
}
}
struct HomeView: View {
var physique: some View {
VStack {
Textual content("House")
.font(.largeTitle)
.padding()
Spacer()
}
}
}
class TabViewModel: ObservableObject {
@Printed var tabs: [TabItem] = [
TabItem(title: "Home", imageName: "house.fill", view: AnyView(HomeView()))
]
}
struct TabItem {
let title: String
let imageName: String
let view: AnyView
}
So I’m making an attempt to determine a correct option to decouple our views from concrete sorts and likewise not use AnyView. However different then code above I couldn’t provide you with compilable code or discover any useful resolution on-line.
Please can anybody clarify to me the way in which to go abut concrete sorts, and dependency injection in SwiftUI in context of above code?
I attempted to make use of protocols like:
protocol TabViewProviding {
var title: String { get }
var imageName: String { get }
var tabView: any View { get }
}
protocol TabViewModelProviding: ObservableObject {
var tabs: [TabViewProviding] { get }
}
and change concrete sorts with generics however greatest i received was errors like
Static technique 'buildExpression' requires that 'Content material' conform to 'AccessibilityRotorContent'
or simply coupling concrete sorts elsewhere.