计算年龄代码单独写成函数
老师你好,我把计算年龄那块的代码单独写成一个函数,代入类型为UIDataPicker,代码如下:
func jisuanage(birthday:UIDatePicker) ->NSInteger{
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now = NSDate()
let components = gregorian?.components(NSCalendarUnit.YearCalendarUnit, fromDate:birthday.date, toDate: now, options: NSCalendarOptions(0))
let age = components?.year
return age!
}
就没问题,但是如果把代入参数类型设置为NSData,然后代码改为:
func jisuanage(birthdayyear:NSData) ->NSInteger{
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now = NSDate()
let components = gregorian?.components(NSCalendarUnit.YearCalendarUnit, fromDate:birthdayyear, toDate: now, options: NSCalendarOptions(0))
let age = components?.year
return age!
}
就编译报错,报错内容:Extra argument 'toDate' in call,不知道是为什么,fromDate后面的参数不应该就是NSDate类型么?