https://developer.apple.com/forums/thread/652064 how can I solve my problem like here or in a different way. When I make the Binding variable generic, I want to access the image property in the Array.
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 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?
}
Model 2:
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?
}
View:
struct QuestionCardViewd: View {
@EnvironmentObject var signQuestionProvider: SignQuestionProvider
@EnvironmentObject var optionConfigure: OptionConfigure
@Binding var questions: [T]
var body: some View {
VStack {
HStack {
Text("\(optionConfigure.step + 1) /")
.bold()
Text("\(optionConfigure.questionCount)")
}
.padding(5) .background(Color(UIColor.secondarySystemBackground))
.cornerRadius(10)
.offset(y: -25)
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 **
.offset(y: CGFloat(relativeIndex * -35))
.opacity(2.5 - Double(relativeIndex))
.scaleEffect(1 - CGFloat(relativeIndex) * 0.1)
)
default:
return AnyView(EmptyView())
}
}
}
.animation(.spring())
OptionView()
}
.onDisappear {
optionConfigure.step = 0
optionConfigure.selected = ""
}
}
}