Search code examples
swiftnsdateformatterios13nsdatecomponentsdateformatter

DateComponentsFormatter Crash On String From Components


I'm getting crash reports from within DateComponentsFormatter. I'm unable to replicate myself so if anyone has a solution or a way to reproduce it I'd be very grateful.

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)

My code calling into it looks like this:

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) ?? "")

I haven't found any combination or dates that throws this kind of an exception. All calls to this function take place on the main thread.


Solution

  • I wrote a little unit test, and it stopped at a test breakpoint with the log:

    Specifying positional units with gaps is ambiguous, and therefore unsupported

    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
                }
            }
        }
    }
    

    Here is some more info:
    enter image description here