NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Form Picker Error: requires that X conform to hashable

Forums > Swift

Within a Form I have a "Your Current Department" Picker for the app user to select their department of employment (Sales, Management, etc.). This Picker's selection pulls from the viewModel.

Here is a link to the code on GitHub.

The following two errors on the ProfileFormView inhibit compilation:

  1. "Generic struct 'Picker' requires that 'Department' conform to 'Hashable'"

  2. "Referencing initializer 'init(_:selection:content:)' on 'Picker' requires that 'Department' conform to 'Hashable'"

struct ProfileFormView: View {

    @Environment(\.colorScheme) var colorScheme
    @Binding var tabSelection: Int
    @EnvironmentObject var vm: EmployeeViewModel
    let hoursWorked = 120...180
    let currentDept = ["Sales","Management","Senior Management","Hospitality","Sanitation","Other"]

    var body: some View {
        NavigationView {
            VStack {
                Form {
                    Section(header: Text("Personal Information:")
                        .foregroundColor(colorScheme == .dark ? Color.white:
                        .bold()) {
                            Picker("Hours per Month", selection: $vm.userData.monthlyHoursWorked) {
                                ForEach(hoursWorked, id: \.self) {
                            //error is below
                            Picker("Your Current Department", selection: $vm.userData.department) {
                                ForEach(currentDept, id: \.self) {
                                    Text ($0)
            .navigationTitle("Employee Information")
            .toolbar {
                NavigationLink(destination: PayRateView(
                    depts: DeptList.deptList), label: {Text("Pay Charts")

The "Department" is a variable within the Employee model:

struct Employee: Identifiable {
    var id = UUID()
    let name: String
    var birthdate: Date
    var hiredate: Date
    var retirementAge: Int
    var department: Department
    var monthlyHoursWorked: Int
    var regularPayRateIncreases: Bool
    var regularPayRateIncreasePercentage: Int
    var regularPayRateIncreaseYearInterval: Int

    private(set) var employmentHistory: [Department]

    init(_ name: String,
         birthdate: Date,
         hiredate: Date,
         retirementAge: Int,
         department: Department = .other("Employee"),
         monthlyHoursWorked: Int,
         regularPayRateIncreases: Bool,
         regularPayRateIncreasePercentage: Int,
         regularPayRateIncreaseYearInterval: Int)
    { = name
        self.birthdate = birthdate
        self.hiredate = hiredate
        self.retirementAge = retirementAge
        self.department = department
        self.monthlyHoursWorked = monthlyHoursWorked
        self.regularPayRateIncreases = regularPayRateIncreases
        self.regularPayRateIncreasePercentage = regularPayRateIncreasePercentage
        self.regularPayRateIncreaseYearInterval = regularPayRateIncreaseYearInterval
        self.employmentHistory = []

If I add Hashable to this struct, I get the following two errors:

  1. "Type 'Employee' does not conform to protocol 'Equatable' "

  2. "Type 'Employee' does not conform to protocol 'Hashable' "


Try to add those methods in your struct like so

    static func ==(lhs: Employee, rhs: Employee) -> Bool {
        return ==

    func hash(into hasher: inout Hasher) {

and add protocols to your struct Employee accordingly.

struct Employee: Identifiable, Equatable, Hashable



Thank you!

Your suggestions removed the errors in my model, but I still have the two errors in the ProfileFormView:

"Generic struct 'Picker' requires that 'Department' conform to 'Hashable'"

"Referencing initializer 'init(_:selection:content:)' on 'Picker' requires that 'Department' conform to 'Hashable'"


As per Hashable protocol you have to do as follows:

  • For a struct, all its stored properties must conform to Hashable.
  • For an enum, all its associated values must conform to Hashable. (An enum without associated values has Hashable conformance even without the declaration.)

You Department type is enum. But the point is that it is a mixture of associated values and cases without them. On the other hand I found such solution workable

hasher.combine(value) // combine with associated value, if it's not Hashable map it to some Hashable type and then combine result

So theoretically this might sovle your problem ->

extension Department: Hashable {
    func hash(into hasher: inout Hasher) {
        switch self {
        case .sales:
        case .management:
        case .seniorManagement:
            hasher.combine("Senior Management")
        case .other(let value):

At least in playgrounds it generates hashValue accordingly.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.