NSDateComponentsFormatter 的 stringFromDate(_, toDate:) 返回 nil [英] NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

查看:91
本文介绍了NSDateComponentsFormatter 的 stringFromDate(_, toDate:) 返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 string 为零?

let formatter = NSDateComponentsFormatter()
let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0)
let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate)
let string = formatter.stringFromDate(referenceDate, toDate: intervalDate)

我期待返回类似6w 10s"的字符串.

I'm expecting a string like "6w 10s" to be returned.

(6 周是 3,628,800 秒.)

(6 weeks is 3,628,800 seconds.)

为了排除故障,我尝试设置 allowedUnits:

To troubleshoot, I tried setting allowedUnits:

formatter.allowedUnits = .YearCalendarUnit | .MonthCalendarUnit | .WeekCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit

导致此错误的原因:

"NSInvalidArgumentException", "指定带有间隙的位置单位不明确,因此不受支持"

"NSInvalidArgumentException", "Specifying positional units with gaps is ambiguous, and therefore unsupported"

我不知道什么是位置单位"(足球之外),而且我不认为我在指定任何差距.

I don't know what a "positional unit" is (outside of football), and I don't think I'm specifying any gaps.

使用 stringFromTimeInterval() 因为这将根据系统的当前日期返回不同的结果.(即,二月一个月只有 28/29 天.)我想从 NSDate 参考日期计算时间间隔.

I'm not using stringFromTimeInterval() because that will return different results depending on the system's current date. (i.e., February only has 28/29 days in a month.) I want to calculate the time interval from the NSDate reference date.

我使用的是 Xcode 6.1.1 (6A2008a).如果有帮助,请查看 Playground 屏幕截图:

I'm using Xcode 6.1.1 (6A2008a). Here's a Playground screenshot if that helps:

推荐答案

来自 headerdoc:

From the headerdoc:

/* Bitmask of units to include. Set to 0 to get the default behavior.
   Note that, especially if the maximum number of units is low, unit
   collapsing is on, or zero dropping is on, not all allowed units may
   actually be used for a given NSDateComponents. Default value is the
   components of the passed-in NSDateComponents object, or years | 
   months | weeks | days | hours | minutes | seconds if passed an
   NSTimeInterval or pair of NSDates.

   Allowed units are:

    NSCalendarUnitYear
    NSCalendarUnitMonth
    NSCalendarUnitWeekOfMonth (used to mean "quantity of weeks")
    NSCalendarUnitDay
    NSCalendarUnitHour
    NSCalendarUnitMinute
    NSCalendarUnitSecond

   Specifying any other NSCalendarUnits will result in an exception.
 */
var allowedUnits: NSCalendarUnit

所以:

let formatter = NSDateComponentsFormatter()
formatter.calendar = NSCalendar.currentCalendar()
formatter.allowedUnits = nil
formatter.allowedUnits |= .CalendarUnitYear
formatter.allowedUnits |= .CalendarUnitMonth
formatter.allowedUnits |= .CalendarUnitWeekOfMonth
formatter.allowedUnits |= .CalendarUnitDay
formatter.allowedUnits |= .CalendarUnitHour
formatter.allowedUnits |= .CalendarUnitMinute
formatter.allowedUnits |= .CalendarUnitSecond

let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0)
let intervalDate = NSDate(timeInterval: 214458810, sinceDate: referenceDate)
let string = formatter.stringFromDate(referenceDate, toDate: intervalDate)
// -> "6y 9m 2w 4d 3:53:30"

<小时>

当我们在中间省略其中之一时:


When we omit one of them in the middle:

formatter.allowedUnits = nil
formatter.allowedUnits |= .CalendarUnitYear
formatter.allowedUnits |= .CalendarUnitMonth
formatter.allowedUnits |= .CalendarUnitWeekOfMonth
formatter.allowedUnits |= .CalendarUnitDay
// formatter.allowedUnits |= .CalendarUnitHour
formatter.allowedUnits |= .CalendarUnitMinute
formatter.allowedUnits |= .CalendarUnitSecond

'指定带有间隙的位置单位是不明确的,因此不受支持' 错误:) 我认为这就是间隙"的意思.

'Specifying positional units with gaps is ambiguous, and therefore unsupported' error :) That is the "gap" means, I think.

这篇关于NSDateComponentsFormatter 的 stringFromDate(_, toDate:) 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆