I have made some progress but still need help. My GitHub repository is updated with new Employee
model and EmployeeViewModel
I'm having difficulty finding the right place to put my retireDate
constant. Here is the EmployeeViewModel
code:
class EmployeeViewModel: ObservableObject {
@Published var userData: Employee =
Employee("Charlotte Grote",
birthdate: DateComponents(calendar: .current, year: 1965, month: 7, day: 20).date!,
hiredate: DateComponents(calendar: .current, year: 2021, month: 12, day: 28).date!,
yearGroup: 1, retirementAge: 65, department: .other("Hospitality"),
monthlyHoursWorked: 160,
planningToPromote1: true,
planningToPromote2: false,
planningToPromote3: false,
planningToPromote4: false,
promotion1Date: DateComponents(calendar: .current, year: 2023, month: 12, day: 28).date!,
promotion2Date: DateComponents(calendar: .current, year: 2025, month: 6, day: 28).date!,
promotion3Date: DateComponents(calendar: .current, year: 2026, month: 12, day: 28).date!,
regularPayRateIncreases: true,
regularPayRateIncreasePercentage: 4,
regularPayRateIncreaseYearInterval: 4)
var firstDeptMonths: Int { Calendar.current.dateComponents([.month], from: userData.hiredate, to: userData.promotion1Date).month! }
var secondDeptMonths: Int { Calendar.current.dateComponents([.month], from: userData.promotion1Date, to: userData.promotion2Date).month! }
var thirdDeptMonths: Int { Calendar.current.dateComponents([.month], from: userData.promotion2Date, to: userData.promotion3Date).month! }
let retireDate = Calendar.current.date(byAdding: .year, value: userData.retirementAge, to: userData.birthdate)! //2 errors here
var fourthDeptMonths: Int { Calendar.current.dateComponents([.month], from: userData.promotion3Date, to: retireDate).month! }
}
extension Date {
var age: Int { Calendar.current.dateComponents([.year], from: self, to: Date()).year! }
var yearGroup: Int { Calendar.current.dateComponents([.year], from: self, to: Date()).year! + 1}
var totalYearsRemaining: Int { Calendar.current.dateComponents([.year], from: self, to: retireDate).year! } //3rd error here
var totalMonthsRemaining: Int { Calendar.current.dateComponents([.month], from: self, to: retireDate).month! } //4th error here
}
The errors are:
Cannot use instance member 'userData' within property initializer; property initializers run before 'self' is available
Cannot use instance member 'userData' within property initializer; property initializers run before 'self' is available
Cannot find 'retireDate' in scope
Cannot find 'retireDate' in scope