I have a Binding variable, I am trying to make it a Generi Array. I will take two different models in it and I want to reach the image property in the model. How can I do that ?
I use two different models in different page transitions. How can I make the array holding the model generic?
Model 1
struct PoliceSignContainer: Codable, Hashable {
var policeQuestions: [PoliceSign]?
}
enum PoliceSignSectionType: String, Codable, Hashable {
case A = "A"
case B = "B"
case C = "C"
}
struct PoliceSign: Codable, Hashable {
var id: Int?
var image: String?
var sections: [PoliceSignSectionType.RawValue : String]?
var correct: String?
}
Model 2
struct TrafficSignContainer: Codable, Hashable {
var trafficQuestions: [TrafficSign]?
}
enum TrafficSignSectionType: String, Codable, Hashable {
case A = "A"
case B = "B"
}
struct TrafficSign: Codable, Hashable {
var id: Int?
var image: String?
var sections: [TrafficSignSectionType.RawValue : String]?
var correct: String?
}
struct QuestionCardView: View {
@EnvironmentObject var optionConfigure: OptionConfigure
@Binding var questions: [Any]
var body: some View {
VStack {
....
ZStack {
ForEach((questions.indices).reversed(), id: \.self) { index -> AnyView in
let relativeIndex = index - optionConfigure.step
switch relativeIndex {
case 0...2:
return AnyView(
ImageCard(image: .constant("\(questions[index].image ?? "p1")")) // here
...
)
default:
return AnyView(EmptyView())
}
}
}
.animation(.spring())
OptionView()
}
...
}
}
