要从NSDateComponents
对象获取分钟值,请执行以下操作:
var someNSDateComponentsObject: NSDateComponents = NSCalendar.currentCalendar().components(timeUnit, fromDate: someDate, toDate: someOtherDate, options: nil)
someNSDateComponentsObject.minute
如果不是someNSDateComponentsObject.minute
,我想使用timeUnit
的值NSCalendarUnit
,该怎么办? switch语句是唯一的选择,还是有更优雅的解决方案?
总的来说,我想要完成的事情是在两个日期之间获得timeUnit
的数量。有没有更好的方法可以避免这个问题?
答案 0 :(得分:2)
In iOS 8 NSDateComponents
gained之前做过的方法valueForComponent:
(及其合作伙伴setValue:forComponent:
),奇怪的是没有进入类引用文档然而。它完成了你所期望的:给定NSCalendarUnit
,它返回日期组件实例为该单元保存的值。