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:
-
"Generic struct 'Picker' requires that 'Department' conform to 'Hashable'"
-
"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: Color.black)
.font(.body)
.bold()) {
Picker("Hours per Month", selection: $vm.userData.monthlyHoursWorked) {
ForEach(hoursWorked, id: \.self) {
Text("\($0)")
}}
//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")
})
}
}
.accentColor(Color(.label))
.navigationViewStyle(StackNavigationViewStyle())
}
}
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)
{
self.name = 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:
-
"Type 'Employee' does not conform to protocol 'Equatable' "
-
"Type 'Employee' does not conform to protocol 'Hashable' "