SwiftUI Generic Binding Array

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 = ""
        }
    }
}

Hi, by looking at the other post I would recommend using a different model structure to achieve what you want. ForEach doesn't know what model you currently have and therefore can't access the values behind them, event if they have the same name. I would create a universal model that has a variable pointing at the type. And based on that type you can decide if you want to show a traffic sign, or a police sign.

enum SignType{
case PoliceSign
case TrafficSign
}
struct Sign: Codable, Identifiable{
var id: UUID
var type: SignType // use enum to define type
var image: String?
...
}

And by making the struct conform to Identifiable you will always have a unique id. Depending an the type you can then access the corresponding selection and solve the issue with mismatching models.

David

SwiftUI Generic Binding Array
 
 
Q