swift – Generic Enum as a Parameter for a Picker SwiftUI iOS


I’m attempting to create a entry subject view that can settle for any enum for a Picker (so long as that enum conforms to CaseIterable and String.

struct EntryFieldView<T: CaseIterable>: View the place T.AllCases: RandomAccessCollection, T.AllCases.Factor: Hashable & RawRepresentable, T.AllCases.Factor.RawValue == String {
    @State var fieldTitle: String = ""
    @State var dropDownSelection: T
    @Binding var entry: String
    var physique: some View {
        VStack(spacing: 0) {
            Group {
                if self.fieldTitle.isEmpty == false {
                    HStack {
                        Textual content(self.fieldTitle)
                            .font(.system(dimension: 10))
                        Spacer()
                    }
                }
                ZStack {
                    Rectangle()
                        .body(peak: 40)
                        .foregroundColor(.white)
                        .shadow(radius: 2)
                    HStack {
                        Picker("Check", choice: self.$entry) {
                            ForEach(self.dropDownSelection.allCases, id: .self) { choice in
                                Textual content(choice.rawValue)
                            }
                        }
                    }
                    .padding([.leading, .trailing], 5)
                }
            }
            .padding(.backside, 10)
        }
    }
}

And for instance I’ve a easy checklist of nations:

enum Nations: String, CaseIterable {
    case UK = "United Kingdom"
    case USA = "United States of America"
    case ES = "Spain"
    case PL = "Poland"
    case DE = "Germany"
}

I get the error Static member 'allCases' can't be used on occasion of sort 'T'
with the suggestion
Static member 'allCases' can't be used on occasion of sort 'T'

If dropDownSelection is of Sort T and T conforms to CaseIterable, why cannot I take advantage of dropDownSelection.allCases, and why wouldn’t it counsel I change dropDownSelection with occasion T?

Thanks

See also  ios - MessageExtension App utilization description error however I've the important thing in my Information.plist

Leave a Reply