【问题标题】:Getting NSDateComponents' value for an NSCalendarUnit in a variable在变量中获取 NSCalendarUnit 的 NSDateComponents 值
【发布时间】:2015-10-22 10:17:12
【问题描述】:

要从 NSDateComponents 对象中获取分钟值,请执行以下操作:

var someNSDateComponentsObject: NSDateComponents = NSCalendar.currentCalendar().components(timeUnit, fromDate: someDate, toDate: someOtherDate, options: nil)
someNSDateComponentsObject.minute

如果我不想使用someNSDateComponentsObject.minute,而是想使用timeUnit 的值,即NSCalendarUnit,该怎么办? switch 语句会是唯一的选择,还是有更优雅的解决方案?

总的来说,我想要完成的事情是在两个日期之间获取timeUnits 的数量。有没有更好的方法来避免这个问题?

【问题讨论】:

    标签: ios swift cocoa-touch nsdatecomponents


    【解决方案1】:

    In iOS 8 NSDateComponents gained how-did-this-not-exist-before 方法valueForComponent:(及其合作伙伴setValue:forComponent:),奇怪的是还没有进入类参考文档。它会如你所愿:给定 NSCalendarUnit,它会返回日期组件实例为该单元保存的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      相关资源
      最近更新 更多