Differences in NSDateComponents syntax?

雨燕双飞 提交于 2019-11-26 15:32:06

Swift 2

The NSCalendarUnit names have changed in Swift 2.

Also, now we have to pass these arguments in an OptionSet, like this:

let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date)

Swift 3

Many things have changed, according to the Swift API Design Guidelines.

Updated syntax:

let date = Date()
let calendar = Calendar.current()
let components = calendar.components([.hour, .minute, .second, .nanosecond], from: date)

Swift 4

Calendar.current is now a property, and .components has been renamed to .dateComponents. Otherwise it's the same as in Swift 3.

let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .second, .nanosecond], from: date)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!