Contents

Duration.UnitsFormatStyle.ZeroValueUnitsDisplayStrategy

A strategy that determines how to format a unit whose value is zero.

Declaration

struct ZeroValueUnitsDisplayStrategy

Overview

When using a Duration.UnitsFormatStyle, specifying a ZeroValueUnitsDisplayStrategy enables you to decide whether show a unit whose value is zero.

The following example creates a duration of 25 seconds, then formats it with two different styles. The first style uses the hide display strategy, which omits the hours and minutes, since their value is 0. The second uses show(length:) to create two-digit representations of the hour and minute fields.

let duration = Duration.seconds(25)
let hide = duration.formatted(
    .units(allowed: [.hours, .minutes, .seconds],
           width: .abbreviated,
           zeroValueUnits:.hide)) // 25 sec
let showTwo = duration.formatted(
    .units(allowed: [.hours, .minutes, .seconds],
           width: .abbreviated,
           zeroValueUnits:.show(length: 2))) // 00 hr, 00 min, 25 sec

Topics

Using common strategies

See Also

Working with zero values