I have written the following computed property to calculate the age of the app user:
var ageCalc: Int {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: .gregorian)
let now = Date()
let calcAge = calendar.components(.year, from: userData.birthdate, to: now, options: [])
let age = calcAge.year
}
The compiler error I receive is:
"Missing return in getter expected to return 'Int'"
Adding "return age" gives a slew of errors:
CodePudding user response:
Using Calendar
instead of the old NSCalendar
you can write the property as
var ageCalc: Int {
Calendar(identifier: .gregorian).dateComponents([.year], from: userData.birthdate, to: .now).year!
}