我刚刚决定将我的swift 2.0代码转换为3.0,但我遇到了Calendar的问题。在swift 2.0中,我这样做是为了计算两个日期之间的差异,但它在Swift 3.0上不再起作用了。我能做什么?感谢
let from = dateArray[indexPath.row]
let now = NSDate()
let components : NSCalendarUnit = [.Second, .Minute, .Hour, .Day, .WeekOfMonth]
let difference = NSCalendar.currentCalendar().components(components, fromDate: from!, toDate: now, options: [])
答案 0 :(得分:1)
原因是很多语法更改,然后有一个函数调用
您应该使用的.compare
:
let from = dateArray[indexPath.row]
let now = NSDate()
let components : NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfMonth]
let difference = NSCalendar.current.compare(from, to: now as Date, toGranularity: .day)
将toGranularity
更改为您想要的值。