swift - DateComponentsFormatter 在组件字符串上崩溃

标签 swift nsdateformatter ios13 nsdatecomponents dateformatter

我从 DateComponentsFormatter 中获取崩溃报告。我无法复制自己,因此如果有人有解决方案或重现它的方法,我将非常感激。

0   CoreFoundation                  0x1a0142a48 __exceptionPreprocess + 220 (NSException.m:199)
1   libobjc.A.dylib                 0x19fe69fa4 objc_exception_throw + 56 (objc-exception.mm:565)
2   Foundation                      0x1a0452e78 -[NSDateComponentsFormatter _canonicalizedDateComponents:withCalendar:usedUnits:withReferenceDate:] + 1436 (NSDateComponentsFormatter.m:101)

我调用它的代码如下所示:

let formatter = DateComponentsFormatter()

// `units` is a user customizable array of NSCalendar.Unit, limited to a subset of [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
formatter.allowedUnits = units

// `style` is a user choosable selection of the DateComponentsFormatter.UnitsStyle enum
formatter.unitsStyle = style

// `components` is a user customizable array of Calendar.Component, limited to a subset of [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
let dateComponents = Calendar.current.dateComponents(components, from: date1, to: date2)

print(formatter.string(from: dateComponents) ?? "")

我还没有发现任何组合或日期引发此类异常。对此函数的所有调用都发生在主线程上。

最佳答案

我编写了一个小单元测试,它停在测试断点处,日志如下:

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

func test_formatter() {

    func randomUnits() -> NSCalendar.Unit {
        let availableUnitsRawValues: [UInt] = [1 << 2, 1 << 3, 1 << 12, 1 << 4, 1 << 5, 1 << 6, 1 << 7] // Corresponding to [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
        let nrUsedUnits = Int.random(in: 1 ... availableUnitsRawValues.count)
        let shuffeledAvailableUnitsRawValues = availableUnitsRawValues.shuffled
        let usedUnitsRawValues = shuffeledAvailableUnitsRawValues[ 0 ..< nrUsedUnits]
        let usedUnitsRawValue = usedUnitsRawValues.reduce(0, |)
        let usedUnits = NSCalendar.Unit.init(rawValue: usedUnitsRawValue)
        return usedUnits
    }

    func randomStyle() -> DateComponentsFormatter.UnitsStyle {
        let randomStyleIndex = Int.random(in: 0 ... 5)
        let randomStyle = DateComponentsFormatter.UnitsStyle.init(rawValue: randomStyleIndex)!
        return randomStyle
    }

    func randomDate() -> Date {
        let randomDayInterval = Double.random(in: -10 ... 10)
        let randomDate = Date.init(timeIntervalSinceNow: randomDayInterval * 24 * 60 * 60)
        return randomDate
    }

    func randomComponents() -> Set<Calendar.Component> {
        let availableComponents: Set<Calendar.Component> = [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
        let nrUsedComponents = Int.random(in: 1 ... availableComponents.count)
        let shuffeledAvailableComponents = Array(availableComponents).shuffled
        let usedComponents = shuffeledAvailableComponents[ 0 ..< nrUsedComponents]
        return Set(usedComponents)
    }


    for _ in 0 ..< 10 {
        let units = randomUnits()
        for _ in 0 ..< 10 {
            let style = randomStyle()
            for _ in 0 ..< 10 {
                let components = randomComponents()
                let formatter = DateComponentsFormatter()
                // `units` is a user customizable array of NSCalendar.Unit, limited to a subset of [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
                formatter.allowedUnits = units
                // `style` is a user choosable selection of the DateComponentsFormatter.UnitsStyle enum
                formatter.unitsStyle = style
                // `components` is a user customizable array of Calendar.Component, limited to a subset of [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
                let dateComponents = Calendar.current.dateComponents(components, 
                                                                                                                         from: randomDate(), 
                                                                                                                         to: randomDate())
                var yyy = formatter.string(from: dateComponents) ?? "" // Crash here
            }
        }
    }
}

这里有更多信息:
enter image description here

关于swift - DateComponentsFormatter 在组件字符串上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59621398/

相关文章:

swift - CloudKit自动后台备份

swift - 使用 Task.all 的示例

json - 转换JSON时间日期格式和位置格式

ios - 如何阻止 ios core-bluetooth CBCentralManagerDelegate 在 ios13 中断开连接

ios - Swift:如何在登录 View 后显示标签栏 Controller

ios - 如何在UserDefaults中保存Double数组?

iphone - iOS 6 NSDateFormatter stringFromDate : empty day of week

ios - 如何从当前日期快速获取接下来的 10 天

swift - 有谁知道如何快速解决 BackgroundTask 问题?

ios - 未调用方法 'application:openURL:options:'