I had the hardest time figuring out how to create NSDateComponents
from a date in Swift. How do you tell NSCalendar
which units you want? You can’t do what you’d do in Objective-C…
I eventually stumbled across a solution. Here’s how to get components from a date in Swift, specifying the NSCalendarUnit
flags:
let date = NSDate()
let unitFlags: NSCalendarUnit = [.Hour, .Day, .Month, .Year]
let components = NSCalendar.currentCalendar().components(unitFlags, fromDate: date)