Sunday, November 27, 2022
HomeiOS Developmentswift - Generic Enum as a Parameter for a Picker SwiftUI iOS

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


I’m attempting to create a entry area view that may 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.Component: Hashable & RawRepresentable, T.AllCases.Component.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(measurement: 10))
                        Spacer()
                    }
                }
                ZStack {
                    Rectangle()
                        .body(peak: 40)
                        .foregroundColor(.white)
                        .shadow(radius: 2)
                    HStack {
                        Picker("Take a look at", 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 International locations: 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 exploit dropDownSelection.allCases, and why wouldn’t it counsel I change dropDownSelection with occasion T?

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments