Calculate age from birth date

前端 未结 4 732
北荒
北荒 2020-12-09 06:34

I cant find age in from birth date. What I got is

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

My code

4条回答
  •  悲哀的现实
    2020-12-09 07:04

    In Swift 2.0+ age computing code should look something like this:

    extension NSDate {
        var age:Int {
            return NSCalendar.currentCalendar()
                .components(NSCalendarUnit.Year, 
                            fromDate: self, 
                            toDate: NSDate(),
                            options: NSCalendarOptions(rawValue: 0)
                .year
        }
    }
    

提交回复
热议问题